2023-02-22 23:40:56 +00:00
|
|
|
package runner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2023-03-07 22:37:45 +00:00
|
|
|
"github.com/cenkalti/backoff/v4"
|
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-06-26 22:03:51 +00:00
|
|
|
// waitForChainStart sets a timeout and repeatedly pings the chains.
|
|
|
|
// If the chain is successfully reached before the timeout, this returns no error.
|
2023-06-20 16:29:25 +00:00
|
|
|
func waitForChainStart(chainDetails ChainDetails) 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
|
2024-04-24 11:41:25 +00:00
|
|
|
if err := backoff.Retry(func() error { return pingZgChain(chainDetails.RpcUrl) }, b); err != nil {
|
2023-06-26 22:03:51 +00:00
|
|
|
return fmt.Errorf("failed connect to chain: %s", err)
|
2023-02-22 23:40:56 +00:00
|
|
|
}
|
2023-06-20 16:29:25 +00:00
|
|
|
|
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-06-20 16:29:25 +00:00
|
|
|
if err := backoff.Retry(func() error { return pingEvm(chainDetails.EvmRpcUrl) }, b); err != nil {
|
2023-06-26 22:03:51 +00:00
|
|
|
return fmt.Errorf("failed connect to chain: %s", err)
|
2023-02-22 23:40:56 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-24 11:41:25 +00:00
|
|
|
func pingZgChain(rpcUrl string) error {
|
2023-06-20 16:29:25 +00:00
|
|
|
statusUrl := fmt.Sprintf("%s/status", rpcUrl)
|
2024-04-24 11:41:25 +00:00
|
|
|
log.Printf("pinging 0g-chain: %s\n", statusUrl)
|
2023-06-20 16:29:25 +00:00
|
|
|
res, err := http.Get(statusUrl)
|
2023-02-22 23:40:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
if res.StatusCode >= 400 {
|
|
|
|
return fmt.Errorf("ping to status failed: %d", res.StatusCode)
|
|
|
|
}
|
2024-04-24 11:41:25 +00:00
|
|
|
log.Println("successfully started ZgChain!")
|
2023-02-22 23:40:56 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-20 16:29:25 +00:00
|
|
|
func pingEvm(evmRpcUrl string) error {
|
2023-02-22 23:40:56 +00:00
|
|
|
log.Println("pinging evm...")
|
2023-06-20 16:29:25 +00:00
|
|
|
res, err := http.Get(evmRpcUrl)
|
2023-02-22 23:40:56 +00:00
|
|
|
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
|
|
|
|
}
|