Skip to content

Commit 6fddff5

Browse files
committed
Switch back to WebSocket URLs in the Eth1Monitor
The HTTP support is not stable enough yet.
1 parent cb3f1fd commit 6fddff5

File tree

2 files changed

+43
-17
lines changed

2 files changed

+43
-17
lines changed

beacon_chain/eth1/eth1_monitor.nim

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -248,16 +248,42 @@ template finalizedDepositsMerkleizer(m: Eth1Monitor): auto =
248248
m.depositsChain.finalizedDepositsMerkleizer
249249

250250
proc fixupWeb3Urls*(web3Url: var string) =
251-
var normalizedUrl = toLowerAscii(web3Url)
252-
if not (normalizedUrl.startsWith("https://") or
253-
normalizedUrl.startsWith("http://") or
254-
normalizedUrl.startsWith("wss://") or
255-
normalizedUrl.startsWith("ws://")):
256-
normalizedUrl = "ws://" & normalizedUrl
257-
warn "The Web3 URL does not specify a protocol. Assuming a WebSocket server", web3Url
258-
259-
# We do this at the end in order to allow the warning above to print the original value
260-
web3Url = normalizedUrl
251+
## Converts HTTP and HTTPS Infura URLs to their WebSocket equivalents
252+
## because we are missing a functional HTTPS client.
253+
let normalizedUrl = toLowerAscii(web3Url)
254+
var pos = 0
255+
256+
template skip(x: string): bool {.dirty.} =
257+
if normalizedUrl.len - pos >= x.len and
258+
normalizedUrl.toOpenArray(pos, pos + x.len - 1) == x:
259+
pos += x.len
260+
true
261+
else:
262+
false
263+
264+
if not (skip("https://") or skip("http://")):
265+
if not (skip("ws://") or skip("wss://")):
266+
web3Url = "ws://" & web3Url
267+
warn "The Web3 URL does not specify a protocol. Assuming a WebSocket server", web3Url
268+
return
269+
270+
block infuraRewrite:
271+
var pos = pos
272+
let network = if skip("mainnet"): mainnet
273+
elif skip("goerli"): goerli
274+
else: break
275+
276+
if not skip(".infura.io/v3/"):
277+
break
278+
279+
template infuraKey: string = normalizedUrl.substr(pos)
280+
281+
web3Url = "wss://" & $network & ".infura.io/ws/v3/" & infuraKey
282+
return
283+
284+
block gethRewrite:
285+
web3Url = "ws://" & normalizedUrl.substr(pos)
286+
warn "Only WebSocket web3 providers are supported. Rewriting URL", web3Url
261287

262288
template toGaugeValue(x: Quantity): int64 =
263289
toGaugeValue(distinctBase x)

tests/test_eth1_monitor.nim

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ suite "Eth1 monitor":
3636

3737
check:
3838
mainnetWssUrl == "wss://mainnet.infura.io/ws/v3/6224f3c792cc443fafb64e70a98f871e"
39-
mainnetHttpUrl == "http://mainnet.infura.io/v3/6224f3c792cc443fafb64e70a98f871e"
40-
mainnetHttpsUrl == "https://mainnet.infura.io/v3/6224f3c792cc443fafb64e70a98f871e"
39+
mainnetHttpUrl == mainnetWssUrl
40+
mainnetHttpsUrl == mainnetWssUrl
4141

4242
goerliWssUrl == "wss://goerli.infura.io/ws/v3/6224f3c792cc443fafb64e70a98f871e"
43-
goerliHttpUrl == "http://goerli.infura.io/v3/6224f3c792cc443fafb64e70a98f871e"
44-
goerliHttpsUrl == "https://goerli.infura.io/v3/6224f3c792cc443fafb64e70a98f871e"
43+
goerliHttpUrl == goerliWssUrl
44+
goerliHttpsUrl == goerliWssUrl
4545

46-
gethHttpUrl == "http://localhost:8545"
47-
gethHttpsUrl == "https://localhost:8545"
48-
unspecifiedProtocolUrl == "ws://localhost:8545"
46+
gethHttpUrl == gethWsUrl
47+
gethHttpsUrl == gethWsUrl
48+
unspecifiedProtocolUrl == gethWsUrl
4949

5050
gethWsUrl == "ws://localhost:8545"

0 commit comments

Comments
 (0)