Skip to content

Commit db1ba54

Browse files
committed
simulators/ethereum/engine: Check eth/engine ports
1 parent db3e65a commit db1ba54

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

simulators/ethereum/engine/testenv.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/binary"
66
"fmt"
77
"math/big"
8+
"net"
89
"net/http"
910
"time"
1011

@@ -114,6 +115,11 @@ func RunTest(testName string, ttd *big.Int, slotsToSafe *big.Int, slotsToFinaliz
114115
}
115116
}()
116117

118+
// Before running the test, make sure Eth and Engine ports are open for the client
119+
if err := CheckEthEngineLive(c); err != nil {
120+
t.Fatalf("FAIL (%s): Ports were never open for client: %v", env.TestName, err)
121+
}
122+
117123
// Setup timeouts
118124
env.Timeout = time.After(timeout)
119125
clMocker.Timeout = time.After(timeout)
@@ -136,10 +142,40 @@ func (t *TestEnv) MainTTD() *big.Int {
136142

137143
func (t *TestEnv) StartClient(clientType string, params hivesim.Params, ttd *big.Int) (*hivesim.Client, *EngineClient, error) {
138144
c := t.T.StartClient(clientType, params, hivesim.WithStaticFiles(t.ClientFiles))
145+
if err := CheckEthEngineLive(c); err != nil {
146+
return nil, nil, fmt.Errorf("Engine/Eth ports were never open for client: %v", err)
147+
}
139148
ec := NewEngineClient(t.T, c, ttd)
140149
return c, ec, nil
141150
}
142151

152+
func CheckEthEngineLive(c *hivesim.Client) error {
153+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
154+
defer cancel()
155+
var (
156+
ticker = time.NewTicker(100 * time.Millisecond)
157+
dialer net.Dialer
158+
)
159+
defer ticker.Stop()
160+
for _, checkport := range []int{EthPortHTTP, EnginePortHTTP} {
161+
addr := fmt.Sprintf("%s:%d", c.IP, checkport)
162+
portcheckloop:
163+
for {
164+
select {
165+
case <-ctx.Done():
166+
return ctx.Err()
167+
case <-ticker.C:
168+
conn, err := dialer.DialContext(ctx, "tcp", addr)
169+
if err == nil {
170+
conn.Close()
171+
break portcheckloop
172+
}
173+
}
174+
}
175+
}
176+
return nil
177+
}
178+
143179
func (t *TestEnv) makeNextTransaction(recipient common.Address, amount *big.Int, payload []byte) *types.Transaction {
144180

145181
gasLimit := uint64(75000)

0 commit comments

Comments
 (0)