2023-06-20 16:29:25 +00:00
|
|
|
package runner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
)
|
|
|
|
|
|
|
|
type KvtoolRunnerConfig struct {
|
2024-05-01 05:53:58 +00:00
|
|
|
ZgChainConfigTemplate string
|
2023-06-20 16:29:25 +00:00
|
|
|
|
|
|
|
ImageTag string
|
|
|
|
IncludeIBC bool
|
|
|
|
|
2024-05-09 18:54:47 +00:00
|
|
|
EnableAutomatedUpgrade bool
|
2024-05-01 05:53:58 +00:00
|
|
|
ZgChainUpgradeName string
|
|
|
|
ZgChainUpgradeHeight int64
|
|
|
|
ZgChainUpgradeBaseImageTag string
|
2023-06-20 16:29:25 +00:00
|
|
|
|
|
|
|
SkipShutdown bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// KvtoolRunner implements a NodeRunner that spins up local chains with kvtool.
|
|
|
|
// It has support for the following:
|
2024-05-01 05:53:58 +00:00
|
|
|
// - running a ZgChain node
|
|
|
|
// - optionally, running an IBC node with a channel opened to the ZgChain node
|
|
|
|
// - optionally, start the ZgChain node on one version and upgrade to another
|
2023-06-20 16:29:25 +00:00
|
|
|
type KvtoolRunner struct {
|
|
|
|
config KvtoolRunnerConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ NodeRunner = &KvtoolRunner{}
|
|
|
|
|
2023-06-26 22:03:51 +00:00
|
|
|
// NewKvtoolRunner creates a new KvtoolRunner.
|
2023-06-20 16:29:25 +00:00
|
|
|
func NewKvtoolRunner(config KvtoolRunnerConfig) *KvtoolRunner {
|
|
|
|
return &KvtoolRunner{
|
|
|
|
config: config,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-26 22:03:51 +00:00
|
|
|
// StartChains implements NodeRunner.
|
|
|
|
// For KvtoolRunner, it sets up, runs, and connects to a local chain via kvtool.
|
2023-06-20 16:29:25 +00:00
|
|
|
func (k *KvtoolRunner) StartChains() Chains {
|
|
|
|
// install kvtool if not already installed
|
|
|
|
installKvtoolCmd := exec.Command("./scripts/install-kvtool.sh")
|
|
|
|
installKvtoolCmd.Stdout = os.Stdout
|
|
|
|
installKvtoolCmd.Stderr = os.Stderr
|
|
|
|
if err := installKvtoolCmd.Run(); err != nil {
|
|
|
|
panic(fmt.Sprintf("failed to install kvtool: %s", err.Error()))
|
|
|
|
}
|
|
|
|
|
|
|
|
// start local test network with kvtool
|
2024-05-01 05:53:58 +00:00
|
|
|
log.Println("starting 0gchain node")
|
|
|
|
kvtoolArgs := []string{"testnet", "bootstrap", "--0gchain.configTemplate", k.config.ZgChainConfigTemplate}
|
2023-06-20 16:29:25 +00:00
|
|
|
// include an ibc chain if desired
|
|
|
|
if k.config.IncludeIBC {
|
|
|
|
kvtoolArgs = append(kvtoolArgs, "--ibc")
|
|
|
|
}
|
|
|
|
// handle automated upgrade functionality, if defined
|
|
|
|
if k.config.EnableAutomatedUpgrade {
|
|
|
|
kvtoolArgs = append(kvtoolArgs,
|
2024-05-01 05:53:58 +00:00
|
|
|
"--upgrade-name", k.config.ZgChainUpgradeName,
|
|
|
|
"--upgrade-height", fmt.Sprint(k.config.ZgChainUpgradeHeight),
|
|
|
|
"--upgrade-base-image-tag", k.config.ZgChainUpgradeBaseImageTag,
|
2023-06-20 16:29:25 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
// start the chain
|
2024-05-01 05:53:58 +00:00
|
|
|
startZgChainCmd := exec.Command("kvtool", kvtoolArgs...)
|
|
|
|
startZgChainCmd.Env = os.Environ()
|
|
|
|
startZgChainCmd.Env = append(startZgChainCmd.Env, fmt.Sprintf("0GCHAIN_TAG=%s", k.config.ImageTag))
|
|
|
|
startZgChainCmd.Stdout = os.Stdout
|
|
|
|
startZgChainCmd.Stderr = os.Stderr
|
|
|
|
log.Println(startZgChainCmd.String())
|
|
|
|
if err := startZgChainCmd.Run(); err != nil {
|
|
|
|
panic(fmt.Sprintf("failed to start 0gchain: %s", err.Error()))
|
2023-06-20 16:29:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// wait for chain to be live.
|
|
|
|
// if an upgrade is defined, this waits for the upgrade to be completed.
|
2024-05-01 05:53:58 +00:00
|
|
|
if err := waitForChainStart(kvtoolZgChainChain); err != nil {
|
2023-06-20 16:29:25 +00:00
|
|
|
k.Shutdown()
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-05-01 05:53:58 +00:00
|
|
|
log.Println("0gchain is started!")
|
2023-06-20 16:29:25 +00:00
|
|
|
|
|
|
|
chains := NewChains()
|
2024-05-01 05:53:58 +00:00
|
|
|
chains.Register("0gchain", &kvtoolZgChainChain)
|
2023-06-20 16:29:25 +00:00
|
|
|
if k.config.IncludeIBC {
|
|
|
|
chains.Register("ibc", &kvtoolIbcChain)
|
|
|
|
}
|
|
|
|
return chains
|
|
|
|
}
|
|
|
|
|
2023-06-26 22:03:51 +00:00
|
|
|
// Shutdown implements NodeRunner.
|
|
|
|
// For KvtoolRunner, it shuts down the local kvtool network.
|
|
|
|
// To prevent shutting down the chain (eg. to preserve logs or examine post-test state)
|
|
|
|
// use the `SkipShutdown` option on the config.
|
2023-06-20 16:29:25 +00:00
|
|
|
func (k *KvtoolRunner) Shutdown() {
|
|
|
|
if k.config.SkipShutdown {
|
|
|
|
log.Printf("would shut down but SkipShutdown is true")
|
|
|
|
return
|
|
|
|
}
|
2024-05-01 05:53:58 +00:00
|
|
|
log.Println("shutting down 0gchain node")
|
|
|
|
shutdownZgChainCmd := exec.Command("kvtool", "testnet", "down")
|
|
|
|
shutdownZgChainCmd.Stdout = os.Stdout
|
|
|
|
shutdownZgChainCmd.Stderr = os.Stderr
|
|
|
|
if err := shutdownZgChainCmd.Run(); err != nil {
|
2023-06-20 16:29:25 +00:00
|
|
|
panic(fmt.Sprintf("failed to shutdown kvtool: %s", err.Error()))
|
|
|
|
}
|
|
|
|
}
|