mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-30 18:25:19 +00:00
7cff7bec23
* add NodeRunner impl for connecting to live network * refactor out node runner setups * remove hardcoded denom for DeployedErc20 * further specify restrictions on DeployedErc20 * don't override .env funded account mnemonic * lower amounts for convert to coin e2e tests * lower fund values used by e2e tests * add doc comments for all e2e functions & types
66 lines
1.8 KiB
Go
66 lines
1.8 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()
|
|
}
|
|
|
|
// waitForChainStart sets a timeout and repeatedly pings the chains.
|
|
// If the chain is successfully reached before the timeout, this returns no error.
|
|
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 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 connect to chain: %s", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func pingKava(rpcUrl string) error {
|
|
statusUrl := fmt.Sprintf("%s/status", rpcUrl)
|
|
log.Printf("pinging kava chain: %s\n", statusUrl)
|
|
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
|
|
}
|