Skip to content

Commit

Permalink
fix: improved stability
Browse files Browse the repository at this point in the history
  • Loading branch information
snobbee committed Dec 8, 2023
1 parent 4f0d1c8 commit 7707aed
Show file tree
Hide file tree
Showing 21 changed files with 168 additions and 88 deletions.
4 changes: 2 additions & 2 deletions scripts/chain-initiator/add-genesis-account.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ func addGenesisAccount(cmdPath, address, balance, homePath string) {

// Execute the command
if err := exec.Command(cmdPath, args...).Run(); err != nil {
log.Fatalf("Command execution failed: %v", err)
log.Fatalf(Red+"Command execution failed: %v", err)
}

// If execution reaches here, the command was successful
log.Printf("add genesis account with address %s, balance: %s and home path %s successfully", address, balance, homePath)
log.Printf(Yellow+"add genesis account with address %s, balance: %s and home path %s successfully", address, balance, homePath)
}
6 changes: 3 additions & 3 deletions scripts/chain-initiator/add-key.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ func addKey(cmdPath, name, homePath, keyringBackend string) string {
// Execute the command
output, err := exec.Command(cmdPath, args...).CombinedOutput()
if err != nil {
log.Fatalf("Command execution failed: %v", err)
log.Fatalf(Red+"Command execution failed: %v", err)
}

// Unmarshal the JSON output
var keyOutput KeyOutput
if err := json.Unmarshal(output, &keyOutput); err != nil {
log.Fatalf("Failed to unmarshal JSON output: %v", err)
log.Fatalf(Red+"Failed to unmarshal JSON output: %v", err)
}

// Log the address
log.Printf("add key with name %s, home path: %s, keyring backend %s and address %s successfully", name, homePath, keyringBackend, keyOutput.Address)
log.Printf(Yellow+"add key with name %s, home path: %s, keyring backend %s and address %s successfully", name, homePath, keyringBackend, keyOutput.Address)

return keyOutput.Address
}
4 changes: 2 additions & 2 deletions scripts/chain-initiator/collect-gentxs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ func collectGentxs(cmdPath, homePath string) {

// Execute the command
if err := exec.Command(cmdPath, args...).Run(); err != nil {
log.Fatalf("Command execution failed: %v", err)
log.Fatalf(Red+"Command execution failed: %v", err)
}

// If execution reaches here, the command was successful
log.Printf("collect gen txs with home path %s successfully", homePath)
log.Printf(Yellow+"collect gen txs with home path %s successfully", homePath)
}
6 changes: 3 additions & 3 deletions scripts/chain-initiator/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ func export(cmdPath, homePath, genesisFilePath string) {
// Execute the command and capture the output
output, err := exec.Command(cmdPath, args...).CombinedOutput()
if err != nil {
log.Fatalf("Command execution failed: %v", err)
log.Fatalf(Red+"Command execution failed: %v", err)
}

// Write the output to the specified file
err = ioutil.WriteFile(genesisFilePath, output, 0644) // nolint: gosec
if err != nil {
log.Fatalf("Failed to write output to file: %v", err)
log.Fatalf(Red+"Failed to write output to file: %v", err)
}

log.Printf("Output successfully written to %s", genesisFilePath)
log.Printf(Yellow+"Output successfully written to %s", genesisFilePath)
}
4 changes: 2 additions & 2 deletions scripts/chain-initiator/gen-tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ func genTx(cmdPath, name, amount, chainId, homePath, keyringBackend string) {

// Execute the command
if err := exec.Command(cmdPath, args...).Run(); err != nil {
log.Fatalf("Command execution failed: %v", err)
log.Fatalf(Red+"Command execution failed: %v", err)
}

// If execution reaches here, the command was successful
log.Printf("gen tx with name %s, amount: %s, chain id %s, home path %s and keyring backend %s successfully", name, amount, chainId, homePath, keyringBackend)
log.Printf(Yellow+"gen tx with name %s, amount: %s, chain id %s, home path %s and keyring backend %s successfully", name, amount, chainId, homePath, keyringBackend)
}
4 changes: 2 additions & 2 deletions scripts/chain-initiator/get-args.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
func getArgs(args []string) (snapshotUrl, newVersion string) {
snapshotUrl = args[0] // https://snapshots.polkachu.com/snapshots/sifchain/sifchain_15048938.tar.lz4
if snapshotUrl == "" {
log.Fatalf("snapshot url is required")
log.Fatalf(Red + "snapshot url is required")
}

newVersion = args[1] // v0.1.0
if newVersion == "" {
log.Fatalf("new version is required")
log.Fatalf(Red + "new version is required")
}

return
Expand Down
28 changes: 23 additions & 5 deletions scripts/chain-initiator/get-flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,37 @@ import (
)

const (
flagHome = "home"
flagCmd = "cmd"
flagHome = "home"
flagCmd = "cmd"
flagSkipSnapshot = "skip-snapshot"
flagSkipChainInit = "skip-chain-init"
flagSkipNodeStart = "skip-node-start"
)

func getFlags(cmd *cobra.Command) (homePath, cmdPath string) {
func getFlags(cmd *cobra.Command) (homePath, cmdPath string, skipSnapshot, skipChainInit, skipNodeStart bool) {
homePath, _ = cmd.Flags().GetString(flagHome)
if homePath == "" {
log.Fatalf("home path is required")
log.Fatalf(Red + "home path is required")
}

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

skipSnapshot, _ = cmd.Flags().GetBool(flagSkipSnapshot)
if skipSnapshot {
log.Printf(Yellow + "skipping snapshot retrieval")
}

skipChainInit, _ = cmd.Flags().GetBool(flagSkipChainInit)
if skipChainInit {
log.Printf(Yellow + "skipping chain init")
}

skipNodeStart, _ = cmd.Flags().GetBool(flagSkipNodeStart)
if skipNodeStart {
log.Printf(Yellow + "skipping node start")
}

return
Expand Down
4 changes: 2 additions & 2 deletions scripts/chain-initiator/init-chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ func initChain(cmdPath, moniker, chainId, homePath string) {

// Execute the command
if err := exec.Command(cmdPath, args...).Run(); err != nil {
log.Fatalf("Command execution failed: %v", err)
log.Fatalf(Red+"Command execution failed: %v", err)
}

// If execution reaches here, the command was successful
log.Printf("init chain with moniker %s, chain id %s and home path: %s successfully", moniker, chainId, homePath)
log.Printf(Yellow+"init chain with moniker %s, chain id %s and home path: %s successfully", moniker, chainId, homePath)
}
85 changes: 48 additions & 37 deletions scripts/chain-initiator/initiator.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,62 +28,70 @@ func main() {
Args: cobra.ExactArgs(2), // Expect exactly 1 argument
Run: func(cmd *cobra.Command, args []string) {
snapshotUrl, newVersion := getArgs(args)
_ = snapshotUrl
homePath, cmdPath := getFlags(cmd)
homePath, cmdPath, skipSnapshot, skipChainInit, skipNodeStart := getFlags(cmd)

// set address prefix
app.SetConfig(false)

// remove home path
removeHome(homePath)
if !skipSnapshot {
// remove home path
removeHome(homePath)

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

// retrieve the snapshot
retrieveSnapshot(snapshotUrl, homePath)
// retrieve the snapshot
retrieveSnapshot(snapshotUrl, homePath)

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

// remove home path
removeHome(homePath)
if !skipChainInit {
// remove home path
removeHome(homePath)

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

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

// add genesis account
addGenesisAccount(cmdPath, validatorAddress, validatorBalance, homePath)
// add genesis account
addGenesisAccount(cmdPath, validatorAddress, validatorBalance, homePath)

// generate genesis tx
genTx(cmdPath, validatorKeyName, validatorSelfDelegation, chainId, homePath, keyringBackend)
// generate genesis tx
genTx(cmdPath, validatorKeyName, validatorSelfDelegation, chainId, homePath, keyringBackend)

// collect genesis txs
collectGentxs(cmdPath, homePath)
// collect genesis txs
collectGentxs(cmdPath, homePath)

// validate genesis
validateGenesis(cmdPath, homePath)
// validate genesis
validateGenesis(cmdPath, homePath)

// update genesis
updateGenesis(validatorBalance, homePath)
// update genesis
updateGenesis(validatorBalance, homePath)
}

// start chain
startCmd := start(cmdPath, homePath)
if !skipNodeStart {
// start chain
startCmd := start(cmdPath, homePath)

// wait for node to start
waitForNodeToStart(node)
// wait for node to start
waitForNodeToStart(node)

// query and calculate upgrade block height
upgradeBlockHeight := queryAndCalcUpgradeBlockHeight(cmdPath, node)
// wait for next block
waitForNextBlock(cmdPath, node)

// submit upgrade proposal
submitUpgradeProposal(cmdPath, validatorKeyName, newVersion, upgradeBlockHeight, homePath, keyringBackend, chainId, node, broadcastMode)
// query and calculate upgrade block height
upgradeBlockHeight := queryAndCalcUpgradeBlockHeight(cmdPath, node)

// listen for signals
listenForSignals(startCmd)
// submit upgrade proposal
submitUpgradeProposal(cmdPath, validatorKeyName, newVersion, upgradeBlockHeight, homePath, keyringBackend, chainId, node, broadcastMode)

// listen for signals
listenForSignals(startCmd)
}
},
}

Expand All @@ -92,8 +100,11 @@ func main() {

rootCmd.PersistentFlags().String(flagCmd, homeEnv+"/go/bin/sifnoded", "path to sifnoded")
rootCmd.PersistentFlags().String(flagHome, homeEnv+"/.sifnoded", "home directory")
rootCmd.PersistentFlags().Bool(flagSkipSnapshot, false, "skip snapshot retrieval")
rootCmd.PersistentFlags().Bool(flagSkipChainInit, false, "skip chain init")
rootCmd.PersistentFlags().Bool(flagSkipNodeStart, false, "skip node start")

if err := rootCmd.Execute(); err != nil {
log.Fatalf("Error executing command: %v", err)
log.Fatalf(Red+"Error executing command: %v", err)
}
}
4 changes: 2 additions & 2 deletions scripts/chain-initiator/listen-for-signals.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ func listenForSignals(cmd *exec.Cmd) {
if cmd != nil && cmd.Process != nil {
err := cmd.Process.Kill()
if err != nil {
log.Fatalf("Failed to kill process: %v", err)
log.Fatalf(Red+"Failed to kill process: %v", err)
}
log.Println("Process killed successfully")
log.Println(Yellow + "Process killed successfully")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ import (

func queryAndCalcUpgradeBlockHeight(cmdPath, node string) string {
// query block height
blockHeight := queryBlockHeight(cmdPath, node)
blockHeight, err := queryBlockHeight(cmdPath, node)
if err != nil {
log.Fatalf(Red+"Failed to query block height: %v", err)
}

// Convert blockHeight from string to int
blockHeightInt, err := strconv.Atoi(blockHeight)
if err != nil {
log.Fatalf("Failed to convert blockHeight to integer: %v", err)
log.Fatalf(Red+"Failed to convert blockHeight to integer: %v", err)
}

// set upgrade block height
Expand Down
9 changes: 4 additions & 5 deletions scripts/chain-initiator/query-block-height.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,24 @@ package main

import (
"encoding/json"
"log"
"os/exec"
)

func queryBlockHeight(cmdPath, node string) string {
func queryBlockHeight(cmdPath, node string) (string, error) {
// Command and arguments
args := []string{"status", "--node", node}

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

// Unmarshal the JSON output
var statusOutput StatusOutput
if err := json.Unmarshal(output, &statusOutput); err != nil {
log.Fatalf("Failed to unmarshal JSON output: %v", err)
return "-1", err
}

return statusOutput.SyncInfo.LatestBlockHeight
return statusOutput.SyncInfo.LatestBlockHeight, nil
}
4 changes: 2 additions & 2 deletions scripts/chain-initiator/remove-home.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ func removeHome(homePath string) {

// Execute the command
if err := exec.Command("rm", args...).Run(); err != nil {
log.Fatalf("Command execution failed: %v", err)
log.Fatalf(Red+"Command execution failed: %v", err)
}

// If execution reaches here, the command was successful
log.Printf("removed home path %s successfully", homePath)
log.Printf(Yellow+"removed home path %s successfully", homePath)
}
4 changes: 2 additions & 2 deletions scripts/chain-initiator/retrieve-snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ func retrieveSnapshot(snapshotUrl, homePath string) {
// 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)
log.Fatalf(Red+"Command execution failed: %v", err)
}

// If execution reaches here, the command was successful
log.Printf("Snapshot retrieved and extracted to path: %s", homePath)
log.Printf(Yellow+"Snapshot retrieved and extracted to path: %s", homePath)
}
2 changes: 1 addition & 1 deletion scripts/chain-initiator/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func start(cmdPath, homePath string) *exec.Cmd {
// Execute the command and stream the output in a goroutine to avoid blocking
go func() {
if err := cmd.Run(); err != nil {
log.Fatalf("Command execution failed: %v", err)
log.Fatalf(Red+"Command execution failed: %v", err)
}
}()

Expand Down
10 changes: 3 additions & 7 deletions scripts/chain-initiator/submit-upgrade-proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,10 @@ func submitUpgradeProposal(cmdPath, name, newVersion, upgradeHeight, homePath, k
}

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

// print the output
log.Printf("%s", output)

// If execution reaches here, the command was successful
log.Printf("Submitted upgrade proposal: %s, upgrade block height: %s", newVersion, upgradeHeight)
log.Printf(Yellow+"Submitted upgrade proposal: %s, upgrade block height: %s", newVersion, upgradeHeight)
}
7 changes: 7 additions & 0 deletions scripts/chain-initiator/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,3 +368,10 @@ type StatusOutput struct {
LatestBlockHeight string `json:"latest_block_height"`
} `json:"SyncInfo"`
}

// Colors
const (
Red = "\033[31m"
Green = "\033[32m"

Check failure on line 375 in scripts/chain-initiator/types.go

View workflow job for this annotation

GitHub Actions / lint

`Green` is unused (deadcode)
Yellow = "\033[33m"
)
Loading

0 comments on commit 7707aed

Please sign in to comment.