Re-enable the HTTP support in Eth1Monitor

This reverts commit 6fddff524c.
This commit is contained in:
Zahary Karadjov 2022-01-15 09:11:17 +02:00 committed by zah
parent af614b6a85
commit ebde027262
2 changed files with 16 additions and 42 deletions

View File

@ -253,42 +253,16 @@ template finalizedDepositsMerkleizer(m: Eth1Monitor): auto =
m.depositsChain.finalizedDepositsMerkleizer m.depositsChain.finalizedDepositsMerkleizer
proc fixupWeb3Urls*(web3Url: var string) = proc fixupWeb3Urls*(web3Url: var string) =
## Converts HTTP and HTTPS Infura URLs to their WebSocket equivalents var normalizedUrl = toLowerAscii(web3Url)
## because we are missing a functional HTTPS client. if not (normalizedUrl.startsWith("https://") or
let normalizedUrl = toLowerAscii(web3Url) normalizedUrl.startsWith("http://") or
var pos = 0 normalizedUrl.startsWith("wss://") or
normalizedUrl.startsWith("ws://")):
normalizedUrl = "ws://" & normalizedUrl
warn "The Web3 URL does not specify a protocol. Assuming a WebSocket server", web3Url
template skip(x: string): bool {.dirty.} = # We do this at the end in order to allow the warning above to print the original value
if normalizedUrl.len - pos >= x.len and web3Url = normalizedUrl
normalizedUrl.toOpenArray(pos, pos + x.len - 1) == x:
pos += x.len
true
else:
false
if not (skip("https://") or skip("http://")):
if not (skip("ws://") or skip("wss://")):
web3Url = "ws://" & web3Url
warn "The Web3 URL does not specify a protocol. Assuming a WebSocket server", web3Url
return
block infuraRewrite:
var pos = pos
let network = if skip("mainnet"): mainnet
elif skip("goerli"): goerli
else: break
if not skip(".infura.io/v3/"):
break
template infuraKey: string = normalizedUrl.substr(pos)
web3Url = "wss://" & $network & ".infura.io/ws/v3/" & infuraKey
return
block gethRewrite:
web3Url = "ws://" & normalizedUrl.substr(pos)
warn "Only WebSocket web3 providers are supported. Rewriting URL", web3Url
template toGaugeValue(x: Quantity): int64 = template toGaugeValue(x: Quantity): int64 =
toGaugeValue(distinctBase x) toGaugeValue(distinctBase x)

View File

@ -36,16 +36,16 @@ suite "Eth1 monitor":
check: check:
mainnetWssUrl == "wss://mainnet.infura.io/ws/v3/6224f3c792cc443fafb64e70a98f871e" mainnetWssUrl == "wss://mainnet.infura.io/ws/v3/6224f3c792cc443fafb64e70a98f871e"
mainnetHttpUrl == mainnetWssUrl mainnetHttpUrl == "http://mainnet.infura.io/v3/6224f3c792cc443fafb64e70a98f871e"
mainnetHttpsUrl == mainnetWssUrl mainnetHttpsUrl == "https://mainnet.infura.io/v3/6224f3c792cc443fafb64e70a98f871e"
goerliWssUrl == "wss://goerli.infura.io/ws/v3/6224f3c792cc443fafb64e70a98f871e" goerliWssUrl == "wss://goerli.infura.io/ws/v3/6224f3c792cc443fafb64e70a98f871e"
goerliHttpUrl == goerliWssUrl goerliHttpUrl == "http://goerli.infura.io/v3/6224f3c792cc443fafb64e70a98f871e"
goerliHttpsUrl == goerliWssUrl goerliHttpsUrl == "https://goerli.infura.io/v3/6224f3c792cc443fafb64e70a98f871e"
gethHttpUrl == gethWsUrl gethHttpUrl == "http://localhost:8545"
gethHttpsUrl == gethWsUrl gethHttpsUrl == "https://localhost:8545"
unspecifiedProtocolUrl == gethWsUrl unspecifiedProtocolUrl == "ws://localhost:8545"
gethWsUrl == "ws://localhost:8545" gethWsUrl == "ws://localhost:8545"