mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-11-10 10:05:18 +00:00
1a223bdce2
* add KavaNodeRunner comments * update kvtool * refactor pingEvm() * refactor pingKava() * refactor EvmRpcPort -> EvmRpcUrl * refactor ChainDetails (g)rpcPort -> (g)rpcUrl * fixup chain details * extract kvtool options to separate config * refactor waitForChainStart() * pull out KavaNodeRunner into kvtool file * rename runner to KvtoolRunner * rename runner.Config to KvtoolRunnerConfig * prefix hardcoded chain details with "kvtool"
64 lines
1.6 KiB
Go
64 lines
1.6 KiB
Go
package runner
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/cenkalti/backoff/v4"
|
|
)
|
|
|
|
// NodeRunner is responsible for starting and managing docker containers to run a node.
|
|
type NodeRunner interface {
|
|
StartChains() Chains
|
|
Shutdown()
|
|
}
|
|
|
|
func waitForChainStart(chainDetails ChainDetails) error {
|
|
// exponential backoff on trying to ping the node, timeout after 30 seconds
|
|
b := backoff.NewExponentialBackOff()
|
|
b.MaxInterval = 5 * time.Second
|
|
b.MaxElapsedTime = 30 * time.Second
|
|
if err := backoff.Retry(func() error { return pingKava(chainDetails.RpcUrl) }, b); err != nil {
|
|
return fmt.Errorf("failed to start & connect to chain: %s", err)
|
|
}
|
|
|
|
b.Reset()
|
|
// the evm takes a bit longer to start up. wait for it to start as well.
|
|
if err := backoff.Retry(func() error { return pingEvm(chainDetails.EvmRpcUrl) }, b); err != nil {
|
|
return fmt.Errorf("failed to start & connect to chain: %s", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func pingKava(rpcUrl string) error {
|
|
log.Println("pinging kava chain...")
|
|
statusUrl := fmt.Sprintf("%s/status", rpcUrl)
|
|
res, err := http.Get(statusUrl)
|
|
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
|
|
}
|
|
|
|
func pingEvm(evmRpcUrl string) error {
|
|
log.Println("pinging evm...")
|
|
res, err := http.Get(evmRpcUrl)
|
|
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
|
|
}
|