2023-02-22 23:40:56 +00:00
|
|
|
package runner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2023-03-07 22:37:45 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2023-02-22 23:40:56 +00:00
|
|
|
"time"
|
|
|
|
|
2023-03-07 22:37:45 +00:00
|
|
|
"github.com/cenkalti/backoff/v4"
|
2023-02-22 23:40:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2023-03-28 22:32:36 +00:00
|
|
|
KavaConfigTemplate string
|
|
|
|
|
2023-03-07 22:37:45 +00:00
|
|
|
ImageTag string
|
|
|
|
IncludeIBC bool
|
2023-03-28 22:32:36 +00:00
|
|
|
|
|
|
|
EnableAutomatedUpgrade bool
|
|
|
|
KavaUpgradeName string
|
|
|
|
KavaUpgradeHeight int64
|
|
|
|
KavaUpgradeBaseImageTag string
|
|
|
|
|
|
|
|
SkipShutdown bool
|
2023-02-22 23:40:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NodeRunner is responsible for starting and managing docker containers to run a node.
|
|
|
|
type NodeRunner interface {
|
2023-03-07 22:37:45 +00:00
|
|
|
StartChains() Chains
|
2023-02-22 23:40:56 +00:00
|
|
|
Shutdown()
|
|
|
|
}
|
|
|
|
|
2023-03-07 22:37:45 +00:00
|
|
|
// KavaNodeRunner manages and runs a single Kava node.
|
|
|
|
type KavaNodeRunner struct {
|
|
|
|
config Config
|
|
|
|
kavaChain *ChainDetails
|
2023-02-22 23:40:56 +00:00
|
|
|
}
|
|
|
|
|
2023-03-07 22:37:45 +00:00
|
|
|
var _ NodeRunner = &KavaNodeRunner{}
|
2023-02-22 23:40:56 +00:00
|
|
|
|
2023-03-07 22:37:45 +00:00
|
|
|
func NewKavaNode(config Config) *KavaNodeRunner {
|
|
|
|
return &KavaNodeRunner{
|
2023-02-22 23:40:56 +00:00
|
|
|
config: config,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-07 22:37:45 +00:00
|
|
|
func (k *KavaNodeRunner) StartChains() Chains {
|
|
|
|
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()))
|
|
|
|
}
|
|
|
|
|
2023-02-22 23:40:56 +00:00
|
|
|
log.Println("starting kava node")
|
2023-03-28 22:32:36 +00:00
|
|
|
kvtoolArgs := []string{"testnet", "bootstrap", "--kava.configTemplate", k.config.KavaConfigTemplate}
|
2023-03-07 22:37:45 +00:00
|
|
|
if k.config.IncludeIBC {
|
|
|
|
kvtoolArgs = append(kvtoolArgs, "--ibc")
|
|
|
|
}
|
2023-03-28 22:32:36 +00:00
|
|
|
if k.config.EnableAutomatedUpgrade {
|
|
|
|
kvtoolArgs = append(kvtoolArgs,
|
|
|
|
"--upgrade-name", k.config.KavaUpgradeName,
|
|
|
|
"--upgrade-height", fmt.Sprint(k.config.KavaUpgradeHeight),
|
|
|
|
"--upgrade-base-image-tag", k.config.KavaUpgradeBaseImageTag,
|
|
|
|
)
|
|
|
|
}
|
2023-03-07 22:37:45 +00:00
|
|
|
startKavaCmd := exec.Command("kvtool", kvtoolArgs...)
|
2023-03-28 22:32:36 +00:00
|
|
|
startKavaCmd.Env = os.Environ()
|
|
|
|
startKavaCmd.Env = append(startKavaCmd.Env, fmt.Sprintf("KAVA_TAG=%s", k.config.ImageTag))
|
2023-03-07 22:37:45 +00:00
|
|
|
startKavaCmd.Stdout = os.Stdout
|
|
|
|
startKavaCmd.Stderr = os.Stderr
|
2023-03-28 22:32:36 +00:00
|
|
|
log.Println(startKavaCmd.String())
|
2023-03-07 22:37:45 +00:00
|
|
|
if err := startKavaCmd.Run(); err != nil {
|
|
|
|
panic(fmt.Sprintf("failed to start kava: %s", err.Error()))
|
2023-02-22 23:40:56 +00:00
|
|
|
}
|
|
|
|
|
2023-03-07 22:37:45 +00:00
|
|
|
k.kavaChain = &kavaChain
|
2023-02-22 23:40:56 +00:00
|
|
|
|
2023-03-07 22:37:45 +00:00
|
|
|
err := k.waitForChainStart()
|
2023-02-22 23:40:56 +00:00
|
|
|
if err != nil {
|
2023-03-07 22:37:45 +00:00
|
|
|
k.Shutdown()
|
|
|
|
panic(err)
|
2023-02-22 23:40:56 +00:00
|
|
|
}
|
2023-03-07 22:37:45 +00:00
|
|
|
log.Println("kava is started!")
|
2023-02-22 23:40:56 +00:00
|
|
|
|
2023-03-07 22:37:45 +00:00
|
|
|
chains := NewChains()
|
|
|
|
chains.Register("kava", k.kavaChain)
|
|
|
|
if k.config.IncludeIBC {
|
|
|
|
chains.Register("ibc", &ibcChain)
|
2023-02-22 23:40:56 +00:00
|
|
|
}
|
2023-03-07 22:37:45 +00:00
|
|
|
return chains
|
|
|
|
}
|
2023-02-22 23:40:56 +00:00
|
|
|
|
2023-03-07 22:37:45 +00:00
|
|
|
func (k *KavaNodeRunner) Shutdown() {
|
2023-03-28 22:32:36 +00:00
|
|
|
if k.config.SkipShutdown {
|
|
|
|
log.Printf("would shut down but SkipShutdown is true")
|
|
|
|
return
|
|
|
|
}
|
2023-03-07 22:37:45 +00:00
|
|
|
log.Println("shutting down kava node")
|
|
|
|
shutdownKavaCmd := exec.Command("kvtool", "testnet", "down")
|
|
|
|
shutdownKavaCmd.Stdout = os.Stdout
|
|
|
|
shutdownKavaCmd.Stderr = os.Stderr
|
|
|
|
if err := shutdownKavaCmd.Run(); err != nil {
|
|
|
|
panic(fmt.Sprintf("failed to shutdown kvtool: %s", err.Error()))
|
2023-02-22 23:40:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-07 22:37:45 +00:00
|
|
|
func (k *KavaNodeRunner) waitForChainStart() error {
|
2023-02-22 23:40:56 +00:00
|
|
|
// exponential backoff on trying to ping the node, timeout after 30 seconds
|
2023-03-07 22:37:45 +00:00
|
|
|
b := backoff.NewExponentialBackOff()
|
|
|
|
b.MaxInterval = 5 * time.Second
|
|
|
|
b.MaxElapsedTime = 30 * time.Second
|
|
|
|
if err := backoff.Retry(k.pingKava, b); err != nil {
|
2023-02-22 23:40:56 +00:00
|
|
|
return fmt.Errorf("failed to start & connect to chain: %s", err)
|
|
|
|
}
|
2023-03-07 22:37:45 +00:00
|
|
|
b.Reset()
|
2023-02-22 23:40:56 +00:00
|
|
|
// the evm takes a bit longer to start up. wait for it to start as well.
|
2023-03-07 22:37:45 +00:00
|
|
|
if err := backoff.Retry(k.pingEvm, b); err != nil {
|
2023-02-22 23:40:56 +00:00
|
|
|
return fmt.Errorf("failed to start & connect to chain: %s", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-07 22:37:45 +00:00
|
|
|
func (k *KavaNodeRunner) pingKava() error {
|
2023-02-22 23:40:56 +00:00
|
|
|
log.Println("pinging kava chain...")
|
2023-03-07 22:37:45 +00:00
|
|
|
url := fmt.Sprintf("http://localhost:%s/status", k.kavaChain.RpcPort)
|
2023-02-22 23:40:56 +00:00
|
|
|
res, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
if res.StatusCode >= 400 {
|
|
|
|
return fmt.Errorf("ping to status failed: %d", res.StatusCode)
|
|
|
|
}
|
|
|
|
log.Println("successfully started Kava!")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-07 22:37:45 +00:00
|
|
|
func (k *KavaNodeRunner) pingEvm() error {
|
2023-02-22 23:40:56 +00:00
|
|
|
log.Println("pinging evm...")
|
2023-03-07 22:37:45 +00:00
|
|
|
url := fmt.Sprintf("http://localhost:%s", k.kavaChain.EvmPort)
|
2023-02-22 23:40:56 +00:00
|
|
|
res, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
// when running, it should respond 405 to a GET request
|
|
|
|
if res.StatusCode != 405 {
|
|
|
|
return fmt.Errorf("ping to evm failed: %d", res.StatusCode)
|
|
|
|
}
|
|
|
|
log.Println("successfully pinged EVM!")
|
|
|
|
return nil
|
|
|
|
}
|