Fix #1784; Handle Infura HTTPS URLs

This commit is contained in:
Zahary Karadjov 2020-11-06 01:11:06 +02:00 committed by zah
parent f596a24c4c
commit a8a66fe3f6
2 changed files with 65 additions and 2 deletions

View File

@ -90,6 +90,38 @@ template depositContractAddress(m: Eth1Monitor): Eth1Address =
template web3Url(m: Eth1Monitor): string =
m.dataProvider.url
proc fixupInfuraUrls*(web3Url: var string) =
## Converts HTTP and HTTPS Infura URLs to their WebSocket equivalents
## because we are missing a functional HTTPS client.
let normalizedUrl = toLowerAscii(web3Url)
var pos = 0
template skip(x: string): bool =
if normalizedUrl.len - pos >= x.len and
normalizedUrl.toOpenArray(pos, pos + x.len - 1) == x:
pos += x.len
true
else:
false
if not (skip("https://") or skip("http://")):
return
let
isMainnet = skip("mainnet")
isGoerli = skip("goerli")
if not (isMainnet or isGoerli):
return
if not skip(".infura.io/v3/"):
return
template infuraKey: string = normalizedUrl.substr(pos)
web3Url = "wss://" & (if isMainnet: "mainnet" else: "goerli") &
".infura.io/ws/v3/" & infuraKey
# TODO: Add preset validation
# MIN_GENESIS_ACTIVE_VALIDATOR_COUNT should be larger than SLOTS_PER_EPOCH
# doAssert SECONDS_PER_ETH1_BLOCK * preset.ETH1_FOLLOW_DISTANCE < GENESIS_DELAY,
@ -357,6 +389,9 @@ proc init*(T: type Eth1Monitor,
web3Url: string,
depositContractAddress: Eth1Address,
depositContractDeployedAt: string): Future[Result[T, string]] {.async.} =
var web3Url = web3Url
fixupInfuraUrls web3Url
let web3 = try: await newWeb3(web3Url)
except CatchableError as err:
debugEcho err.msg

View File

@ -8,5 +8,33 @@ import
suite "Eth1 Chain":
discard
suite "Mainchain monitor":
discard
suite "Eth1 monitor":
test "Rewrite HTTPS Infura URLs":
var
mainnetWssUrl = "wss://mainnet.infura.io/ws/v3/6224f3c792cc443fafb64e70a98f871e"
mainnetHttpUrl = "http://mainnet.infura.io/v3/6224f3c792cc443fafb64e70a98f871e"
mainnetHttpsUrl = "https://mainnet.infura.io/v3/6224f3c792cc443fafb64e70a98f871e"
goerliWssUrl = "wss://goerli.infura.io/ws/v3/6224f3c792cc443fafb64e70a98f871e"
goerliHttpUrl = "http://goerli.infura.io/v3/6224f3c792cc443fafb64e70a98f871e"
goerliHttpsUrl = "https://goerli.infura.io/v3/6224f3c792cc443fafb64e70a98f871e"
gethUrl = "ws://localhost:8545"
fixupInfuraUrls mainnetWssUrl
fixupInfuraUrls mainnetHttpUrl
fixupInfuraUrls mainnetHttpsUrl
fixupInfuraUrls goerliWssUrl
fixupInfuraUrls goerliHttpUrl
fixupInfuraUrls goerliHttpsUrl
fixupInfuraUrls gethUrl
check:
mainnetWssUrl == "wss://mainnet.infura.io/ws/v3/6224f3c792cc443fafb64e70a98f871e"
mainnetHttpUrl == mainnetWssUrl
mainnetHttpsUrl == mainnetWssUrl
goerliWssUrl == "wss://goerli.infura.io/ws/v3/6224f3c792cc443fafb64e70a98f871e"
goerliHttpUrl == goerliWssUrl
goerliHttpsUrl == goerliWssUrl
gethUrl == "ws://localhost:8545"