diff --git a/scripts/chain-initiator/add-genesis-account.go b/scripts/chain-initiator/add-genesis-account.go index 6deed9611c..2dbda9f743 100644 --- a/scripts/chain-initiator/add-genesis-account.go +++ b/scripts/chain-initiator/add-genesis-account.go @@ -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) } diff --git a/scripts/chain-initiator/add-key.go b/scripts/chain-initiator/add-key.go index 288053f2a7..0ca308b829 100644 --- a/scripts/chain-initiator/add-key.go +++ b/scripts/chain-initiator/add-key.go @@ -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 } diff --git a/scripts/chain-initiator/collect-gentxs.go b/scripts/chain-initiator/collect-gentxs.go index 07fe9834ad..7393f132a1 100644 --- a/scripts/chain-initiator/collect-gentxs.go +++ b/scripts/chain-initiator/collect-gentxs.go @@ -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) } diff --git a/scripts/chain-initiator/export.go b/scripts/chain-initiator/export.go index 81fd086568..efb502207c 100644 --- a/scripts/chain-initiator/export.go +++ b/scripts/chain-initiator/export.go @@ -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) } diff --git a/scripts/chain-initiator/gen-tx.go b/scripts/chain-initiator/gen-tx.go index 72805d7274..55b79e73e1 100644 --- a/scripts/chain-initiator/gen-tx.go +++ b/scripts/chain-initiator/gen-tx.go @@ -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) } diff --git a/scripts/chain-initiator/get-args.go b/scripts/chain-initiator/get-args.go index 76d0af533d..bf3a95fb58 100644 --- a/scripts/chain-initiator/get-args.go +++ b/scripts/chain-initiator/get-args.go @@ -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 diff --git a/scripts/chain-initiator/get-flags.go b/scripts/chain-initiator/get-flags.go index 2dd7904469..790b1b33dd 100644 --- a/scripts/chain-initiator/get-flags.go +++ b/scripts/chain-initiator/get-flags.go @@ -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 diff --git a/scripts/chain-initiator/init-chain.go b/scripts/chain-initiator/init-chain.go index 3beaa6b1cf..fb1f09b691 100644 --- a/scripts/chain-initiator/init-chain.go +++ b/scripts/chain-initiator/init-chain.go @@ -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) } diff --git a/scripts/chain-initiator/initiator.go b/scripts/chain-initiator/initiator.go index 0434d8dd2c..5f0c7e1744 100644 --- a/scripts/chain-initiator/initiator.go +++ b/scripts/chain-initiator/initiator.go @@ -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) + } }, } @@ -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) } } diff --git a/scripts/chain-initiator/listen-for-signals.go b/scripts/chain-initiator/listen-for-signals.go index 910257be6e..7ec26f26fc 100644 --- a/scripts/chain-initiator/listen-for-signals.go +++ b/scripts/chain-initiator/listen-for-signals.go @@ -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") } } diff --git a/scripts/chain-initiator/query-and-calc-upgrade-block-height.go b/scripts/chain-initiator/query-and-calc-upgrade-block-height.go index f9ee37c41b..4ead6e29fa 100644 --- a/scripts/chain-initiator/query-and-calc-upgrade-block-height.go +++ b/scripts/chain-initiator/query-and-calc-upgrade-block-height.go @@ -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 diff --git a/scripts/chain-initiator/query-block-height.go b/scripts/chain-initiator/query-block-height.go index 8cb8cc8179..757afad241 100644 --- a/scripts/chain-initiator/query-block-height.go +++ b/scripts/chain-initiator/query-block-height.go @@ -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 } diff --git a/scripts/chain-initiator/remove-home.go b/scripts/chain-initiator/remove-home.go index c370e28995..6a67c29d05 100644 --- a/scripts/chain-initiator/remove-home.go +++ b/scripts/chain-initiator/remove-home.go @@ -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) } diff --git a/scripts/chain-initiator/retrieve-snapshot.go b/scripts/chain-initiator/retrieve-snapshot.go index c4f8fbd4ec..f72833ff08 100644 --- a/scripts/chain-initiator/retrieve-snapshot.go +++ b/scripts/chain-initiator/retrieve-snapshot.go @@ -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) } diff --git a/scripts/chain-initiator/start.go b/scripts/chain-initiator/start.go index 20349c6bf4..c56477be8d 100644 --- a/scripts/chain-initiator/start.go +++ b/scripts/chain-initiator/start.go @@ -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) } }() diff --git a/scripts/chain-initiator/submit-upgrade-proposal.go b/scripts/chain-initiator/submit-upgrade-proposal.go index 468db9ee56..182f3ee545 100644 --- a/scripts/chain-initiator/submit-upgrade-proposal.go +++ b/scripts/chain-initiator/submit-upgrade-proposal.go @@ -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) } diff --git a/scripts/chain-initiator/types.go b/scripts/chain-initiator/types.go index 86801a5b78..6b332b33a9 100644 --- a/scripts/chain-initiator/types.go +++ b/scripts/chain-initiator/types.go @@ -368,3 +368,10 @@ type StatusOutput struct { LatestBlockHeight string `json:"latest_block_height"` } `json:"SyncInfo"` } + +// Colors +const ( + Red = "\033[31m" + Green = "\033[32m" + Yellow = "\033[33m" +) diff --git a/scripts/chain-initiator/update-genesis.go b/scripts/chain-initiator/update-genesis.go index 45695fcf4e..63336bd2d7 100644 --- a/scripts/chain-initiator/update-genesis.go +++ b/scripts/chain-initiator/update-genesis.go @@ -10,13 +10,13 @@ import ( func updateGenesis(validatorBalance, homePath string) { genesis, err := readGenesisFile(genesisFilePath) if err != nil { - log.Fatalf("Error reading genesis file: %v", err) + log.Fatalf(Red+"Error reading genesis file: %v", err) } genesisInitFilePath := homePath + "/config/genesis.json" genesisInit, err := readGenesisFile(genesisInitFilePath) if err != nil { - log.Fatalf("Error reading initial genesis file: %v", err) + log.Fatalf(Red+"Error reading initial genesis file: %v", err) } filterAccountAddresses := []string{ @@ -36,7 +36,7 @@ func updateGenesis(validatorBalance, homePath string) { newValidatorBalance, ok := sdk.NewIntFromString(validatorBalance) if !ok { - panic("invalid number") + panic(Red + "invalid number") } newValidatorBalanceCoin := sdk.NewCoin("rowan", newValidatorBalance) @@ -61,8 +61,12 @@ func updateGenesis(validatorBalance, homePath string) { // set genutil from genesisInit genesis.AppState.Genutil = genesisInit.AppState.Genutil + // update voting period + genesis.AppState.Gov.VotingParams.VotingPeriod = "60s" + genesis.AppState.Gov.DepositParams.MaxDepositPeriod = "60s" + outputFilePath := homePath + "/config/genesis.json" if err := writeGenesisFile(outputFilePath, genesis); err != nil { - log.Fatalf("Error writing genesis file: %v", err) + log.Fatalf(Red+"Error writing genesis file: %v", err) } } diff --git a/scripts/chain-initiator/validate-genesis.go b/scripts/chain-initiator/validate-genesis.go index a8e24d02fe..b32eeee608 100644 --- a/scripts/chain-initiator/validate-genesis.go +++ b/scripts/chain-initiator/validate-genesis.go @@ -11,9 +11,9 @@ func validateGenesis(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("validate genesis with home path %s successfully", homePath) + log.Printf(Yellow+"validate genesis with home path %s successfully", homePath) } diff --git a/scripts/chain-initiator/wait-for-next-block.go b/scripts/chain-initiator/wait-for-next-block.go new file mode 100644 index 0000000000..d163cc9e81 --- /dev/null +++ b/scripts/chain-initiator/wait-for-next-block.go @@ -0,0 +1,42 @@ +package main + +import ( + "log" + "strconv" + "time" +) + +func waitForNextBlock(cmdPath, node string) { + var currentBlockHeight, newBlockHeight int + var err error + + // First, get the current block height + for { + var blockHeightStr string + blockHeightStr, err = queryBlockHeight(cmdPath, node) + if err == nil { + currentBlockHeight, err = strconv.Atoi(blockHeightStr) + if err == nil && currentBlockHeight > 0 { + break + } + } + time.Sleep(5 * time.Second) // Wait 5 second before retrying + } + + log.Printf(Yellow+"Current Block Height: %d", currentBlockHeight) + + // Now, wait for the block height to increase + for { + var blockHeightStr string + blockHeightStr, err = queryBlockHeight(cmdPath, node) + if err == nil { + newBlockHeight, err = strconv.Atoi(blockHeightStr) + if err == nil && newBlockHeight > currentBlockHeight { + break + } + } + time.Sleep(5 * time.Second) // Wait a second before retrying + } + + log.Printf(Yellow+"New Block Height: %d", newBlockHeight) +} diff --git a/scripts/chain-initiator/wait-for-node-to-start.go b/scripts/chain-initiator/wait-for-node-to-start.go index debc811f8b..9cc25c658a 100644 --- a/scripts/chain-initiator/wait-for-node-to-start.go +++ b/scripts/chain-initiator/wait-for-node-to-start.go @@ -12,10 +12,10 @@ func waitForNodeToStart(node string) { // Wait for the node to be running with timout for !isNodeRunning(node) { if time.Since(start) > timeout { - log.Fatalf("Node did not start within the specified timeout") + log.Fatalf(Red + "Node did not start within the specified timeout") } - log.Println("Waiting for node to start...") + log.Println(Yellow + "Waiting for node to start...") time.Sleep(5 * time.Second) } - log.Println("Node is running.") + log.Println(Yellow + "Node is running.") }