Skip to content

Commit 5e45e74

Browse files
committed
Rewrite all HTTP(S) web3 URLs to WebSocket URls
1 parent 87c92ea commit 5e45e74

File tree

2 files changed

+39
-23
lines changed

2 files changed

+39
-23
lines changed

beacon_chain/eth1_monitor.nim

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,13 @@ template depositContractAddress(m: Eth1Monitor): Eth1Address =
9090
template web3Url(m: Eth1Monitor): string =
9191
m.dataProvider.url
9292

93-
proc fixupInfuraUrls*(web3Url: var string) =
93+
proc fixupWeb3Urls*(web3Url: var string) =
9494
## Converts HTTP and HTTPS Infura URLs to their WebSocket equivalents
9595
## because we are missing a functional HTTPS client.
9696
let normalizedUrl = toLowerAscii(web3Url)
9797
var pos = 0
9898

99-
template skip(x: string): bool =
99+
template skip(x: string): bool {.dirty.} =
100100
if normalizedUrl.len - pos >= x.len and
101101
normalizedUrl.toOpenArray(pos, pos + x.len - 1) == x:
102102
pos += x.len
@@ -105,22 +105,28 @@ proc fixupInfuraUrls*(web3Url: var string) =
105105
false
106106

107107
if not (skip("https://") or skip("http://")):
108+
if not (skip("ws://") or skip("wss://")):
109+
web3Url = "ws://" & web3Url
110+
warn "The Web3 URL does not specify a protocol. Assuming a WebSocket server", web3Url
108111
return
109112

110-
let
111-
isMainnet = skip("mainnet")
112-
isGoerli = skip("goerli")
113+
block infuraRewrite:
114+
var pos = pos
115+
let network = if skip("mainnet"): mainnet
116+
elif skip("goerli"): goerli
117+
else: break
113118

114-
if not (isMainnet or isGoerli):
115-
return
119+
if not skip(".infura.io/v3/"):
120+
break
116121

117-
if not skip(".infura.io/v3/"):
118-
return
122+
template infuraKey: string = normalizedUrl.substr(pos)
119123

120-
template infuraKey: string = normalizedUrl.substr(pos)
124+
web3Url = "wss://" & $network & ".infura.io/ws/v3/" & infuraKey
125+
return
121126

122-
web3Url = "wss://" & (if isMainnet: "mainnet" else: "goerli") &
123-
".infura.io/ws/v3/" & infuraKey
127+
block gethRewrite:
128+
web3Url = "ws://" & normalizedUrl.substr(pos)
129+
warn "Only WebSocket web3 providers are supported. Rewriting URL", web3Url
124130

125131
# TODO: Add preset validation
126132
# MIN_GENESIS_ACTIVE_VALIDATOR_COUNT should be larger than SLOTS_PER_EPOCH
@@ -392,7 +398,7 @@ proc init*(T: type Eth1Monitor,
392398
depositContractDeployedAt: string,
393399
eth1Network: Option[Eth1Network]): Future[Result[T, string]] {.async.} =
394400
var web3Url = web3Url
395-
fixupInfuraUrls web3Url
401+
fixupWeb3Urls web3Url
396402

397403
let web3 = try: await newWeb3(web3Url)
398404
except CatchableError as err:

tests/test_eth1_monitor.nim

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,21 @@ suite "Eth1 monitor":
1717
goerliWssUrl = "wss://goerli.infura.io/ws/v3/6224f3c792cc443fafb64e70a98f871e"
1818
goerliHttpUrl = "http://goerli.infura.io/v3/6224f3c792cc443fafb64e70a98f871e"
1919
goerliHttpsUrl = "https://goerli.infura.io/v3/6224f3c792cc443fafb64e70a98f871e"
20-
gethUrl = "ws://localhost:8545"
21-
22-
fixupInfuraUrls mainnetWssUrl
23-
fixupInfuraUrls mainnetHttpUrl
24-
fixupInfuraUrls mainnetHttpsUrl
25-
fixupInfuraUrls goerliWssUrl
26-
fixupInfuraUrls goerliHttpUrl
27-
fixupInfuraUrls goerliHttpsUrl
28-
fixupInfuraUrls gethUrl
20+
gethHttpUrl = "http://localhost:8545"
21+
gethHttpsUrl = "https://localhost:8545"
22+
gethWsUrl = "ws://localhost:8545"
23+
unspecifiedProtocolUrl = "localhost:8545"
24+
25+
fixupWeb3Urls mainnetWssUrl
26+
fixupWeb3Urls mainnetHttpUrl
27+
fixupWeb3Urls mainnetHttpsUrl
28+
fixupWeb3Urls goerliWssUrl
29+
fixupWeb3Urls goerliHttpUrl
30+
fixupWeb3Urls goerliHttpsUrl
31+
fixupWeb3Urls gethHttpUrl
32+
fixupWeb3Urls gethHttpsUrl
33+
fixupWeb3Urls gethWsUrl
34+
fixupWeb3Urls unspecifiedProtocolUrl
2935

3036
check:
3137
mainnetWssUrl == "wss://mainnet.infura.io/ws/v3/6224f3c792cc443fafb64e70a98f871e"
@@ -36,5 +42,9 @@ suite "Eth1 monitor":
3642
goerliHttpUrl == goerliWssUrl
3743
goerliHttpsUrl == goerliWssUrl
3844

39-
gethUrl == "ws://localhost:8545"
45+
gethHttpUrl == gethWsUrl
46+
gethHttpsUrl == gethWsUrl
47+
unspecifiedProtocolUrl == gethWsUrl
48+
49+
gethWsUrl == "ws://localhost:8545"
4050

0 commit comments

Comments
 (0)