-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
65 lines (58 loc) · 1.76 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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 pingFury(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 pingFury(rpcUrl string) error {
statusUrl := fmt.Sprintf("%s/status", rpcUrl)
log.Printf("pinging fury 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 Fury!")
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
}