Skip to content

Commit

Permalink
[TT-536] Geth WS Getting HTTP Handshake Flake (#724)
Browse files Browse the repository at this point in the history
* [TT-536] Fix ws port when multiple are present
  • Loading branch information
tateexon authored Oct 10, 2023
1 parent 649c6d7 commit eb58161
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
1 change: 1 addition & 0 deletions docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func CreateNetwork(l zerolog.Logger) (*tc.DockerNetwork, error) {
NetworkRequest: tc.NetworkRequest{
Name: networkName,
CheckDuplicate: true,
EnableIPv6: false,
},
})
if err != nil {
Expand Down
26 changes: 22 additions & 4 deletions docker/test_env/geth.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (g *Geth) StartContainer() (blockchain.EVMNetwork, InternalDockerUrls, erro
if err != nil {
return blockchain.EVMNetwork{}, InternalDockerUrls{}, err
}
wsPort, err := ct.MappedPort(context.Background(), NatPort(TX_GETH_WS_PORT))
wsPort, err := getUniqueWsPort(context.Background(), ct, TX_GETH_HTTP_PORT, TX_GETH_WS_PORT, g.l)
if err != nil {
return blockchain.EVMNetwork{}, InternalDockerUrls{}, err
}
Expand Down Expand Up @@ -131,6 +131,25 @@ func (g *Geth) StartContainer() (blockchain.EVMNetwork, InternalDockerUrls, erro
return networkConfig, internalDockerUrls, nil
}

func getUniqueWsPort(ctx context.Context, ct tc.Container, httpInternalPort, wsInternalPort string, l zerolog.Logger) (nat.Port, error) {
p, err := ct.Ports(ctx)
if err != nil {
return "", err
}
l.Info().
Interface("ports", p).
Msg("Ports mapped")
httpPorts := p[NatPort(httpInternalPort)]
wsPorts := p[NatPort(wsInternalPort)]
wsPort := NatPort(wsPorts[0].HostPort)
if len(wsPorts) > 1 {
if httpPorts[0].HostPort == wsPorts[0].HostPort || httpPorts[1].HostPort == wsPorts[0].HostPort {
wsPort = NatPort(wsPorts[1].HostPort)
}
}
return wsPort, nil
}

func (g *Geth) getGethContainerRequest(networks []string) (*tc.ContainerRequest, *keystore.KeyStore, *accounts.Account, error) {
chainId := "1337"
blocktime := "1"
Expand Down Expand Up @@ -301,13 +320,12 @@ func (w *WebSocketStrategy) WaitUntilReady(ctx context.Context, target tcwait.St
w.l.Error().Msg("Failed to get the target host")
return err
}
mappedPort, err := target.MappedPort(ctx, w.Port)
wsPort, err := getUniqueWsPort(ctx, target.(tc.Container), TX_GETH_HTTP_PORT, w.Port.Port(), w.l)
if err != nil {
w.l.Error().Msg("Failed to get the mapped ws port")
return err
}

url := fmt.Sprintf("ws://%s:%s", host, mappedPort.Port())
url := fmt.Sprintf("ws://%s:%s", host, wsPort.Port())
w.l.Info().Msgf("Attempting to dial %s", url)
client, err = rpc.DialContext(ctx, url)
if err == nil {
Expand Down
4 changes: 2 additions & 2 deletions docker/test_env/non_dev_geth.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,11 +309,11 @@ func (g *NonDevGethNode) ConnectToClient() error {
if err != nil {
return err
}
port = NatPort(TX_NON_DEV_GETH_WS_PORT)
wsPort, err := ct.MappedPort(context.Background(), port)
wsPort, err := getUniqueWsPort(context.Background(), ct, TX_GETH_HTTP_PORT, TX_NON_DEV_GETH_WS_PORT, g.l)
if err != nil {
return err
}

g.ExternalHttpUrl = fmt.Sprintf("http://%s:%s", host, httpPort.Port())
g.InternalHttpUrl = fmt.Sprintf("http://%s:%s", g.ContainerName, TX_GETH_HTTP_PORT)
g.ExternalWsUrl = fmt.Sprintf("ws://%s:%s", host, wsPort.Port())
Expand Down

0 comments on commit eb58161

Please sign in to comment.