Rewrite all HTTP(S) web3 URLs to WebSocket URls

This commit is contained in:
Zahary Karadjov 2020-11-12 15:49:13 +02:00 committed by zah
parent 87c92ea05f
commit 5e45e7429e
2 changed files with 39 additions and 23 deletions

View File

@ -90,13 +90,13 @@ template depositContractAddress(m: Eth1Monitor): Eth1Address =
template web3Url(m: Eth1Monitor): string = template web3Url(m: Eth1Monitor): string =
m.dataProvider.url m.dataProvider.url
proc fixupInfuraUrls*(web3Url: var string) = proc fixupWeb3Urls*(web3Url: var string) =
## Converts HTTP and HTTPS Infura URLs to their WebSocket equivalents ## Converts HTTP and HTTPS Infura URLs to their WebSocket equivalents
## because we are missing a functional HTTPS client. ## because we are missing a functional HTTPS client.
let normalizedUrl = toLowerAscii(web3Url) let normalizedUrl = toLowerAscii(web3Url)
var pos = 0 var pos = 0
template skip(x: string): bool = template skip(x: string): bool {.dirty.} =
if normalizedUrl.len - pos >= x.len and if normalizedUrl.len - pos >= x.len and
normalizedUrl.toOpenArray(pos, pos + x.len - 1) == x: normalizedUrl.toOpenArray(pos, pos + x.len - 1) == x:
pos += x.len pos += x.len
@ -105,22 +105,28 @@ proc fixupInfuraUrls*(web3Url: var string) =
false false
if not (skip("https://") or skip("http://")): 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 return
let block infuraRewrite:
isMainnet = skip("mainnet") var pos = pos
isGoerli = skip("goerli") let network = if skip("mainnet"): mainnet
elif skip("goerli"): goerli
else: break
if not (isMainnet or isGoerli): if not skip(".infura.io/v3/"):
break
template infuraKey: string = normalizedUrl.substr(pos)
web3Url = "wss://" & $network & ".infura.io/ws/v3/" & infuraKey
return return
if not skip(".infura.io/v3/"): block gethRewrite:
return web3Url = "ws://" & normalizedUrl.substr(pos)
warn "Only WebSocket web3 providers are supported. Rewriting URL", web3Url
template infuraKey: string = normalizedUrl.substr(pos)
web3Url = "wss://" & (if isMainnet: "mainnet" else: "goerli") &
".infura.io/ws/v3/" & infuraKey
# TODO: Add preset validation # TODO: Add preset validation
# MIN_GENESIS_ACTIVE_VALIDATOR_COUNT should be larger than SLOTS_PER_EPOCH # MIN_GENESIS_ACTIVE_VALIDATOR_COUNT should be larger than SLOTS_PER_EPOCH
@ -392,7 +398,7 @@ proc init*(T: type Eth1Monitor,
depositContractDeployedAt: string, depositContractDeployedAt: string,
eth1Network: Option[Eth1Network]): Future[Result[T, string]] {.async.} = eth1Network: Option[Eth1Network]): Future[Result[T, string]] {.async.} =
var web3Url = web3Url var web3Url = web3Url
fixupInfuraUrls web3Url fixupWeb3Urls web3Url
let web3 = try: await newWeb3(web3Url) let web3 = try: await newWeb3(web3Url)
except CatchableError as err: except CatchableError as err:

View File

@ -17,15 +17,21 @@ suite "Eth1 monitor":
goerliWssUrl = "wss://goerli.infura.io/ws/v3/6224f3c792cc443fafb64e70a98f871e" goerliWssUrl = "wss://goerli.infura.io/ws/v3/6224f3c792cc443fafb64e70a98f871e"
goerliHttpUrl = "http://goerli.infura.io/v3/6224f3c792cc443fafb64e70a98f871e" goerliHttpUrl = "http://goerli.infura.io/v3/6224f3c792cc443fafb64e70a98f871e"
goerliHttpsUrl = "https://goerli.infura.io/v3/6224f3c792cc443fafb64e70a98f871e" goerliHttpsUrl = "https://goerli.infura.io/v3/6224f3c792cc443fafb64e70a98f871e"
gethUrl = "ws://localhost:8545" gethHttpUrl = "http://localhost:8545"
gethHttpsUrl = "https://localhost:8545"
gethWsUrl = "ws://localhost:8545"
unspecifiedProtocolUrl = "localhost:8545"
fixupInfuraUrls mainnetWssUrl fixupWeb3Urls mainnetWssUrl
fixupInfuraUrls mainnetHttpUrl fixupWeb3Urls mainnetHttpUrl
fixupInfuraUrls mainnetHttpsUrl fixupWeb3Urls mainnetHttpsUrl
fixupInfuraUrls goerliWssUrl fixupWeb3Urls goerliWssUrl
fixupInfuraUrls goerliHttpUrl fixupWeb3Urls goerliHttpUrl
fixupInfuraUrls goerliHttpsUrl fixupWeb3Urls goerliHttpsUrl
fixupInfuraUrls gethUrl fixupWeb3Urls gethHttpUrl
fixupWeb3Urls gethHttpsUrl
fixupWeb3Urls gethWsUrl
fixupWeb3Urls unspecifiedProtocolUrl
check: check:
mainnetWssUrl == "wss://mainnet.infura.io/ws/v3/6224f3c792cc443fafb64e70a98f871e" mainnetWssUrl == "wss://mainnet.infura.io/ws/v3/6224f3c792cc443fafb64e70a98f871e"
@ -36,5 +42,9 @@ suite "Eth1 monitor":
goerliHttpUrl == goerliWssUrl goerliHttpUrl == goerliWssUrl
goerliHttpsUrl == goerliWssUrl goerliHttpsUrl == goerliWssUrl
gethUrl == "ws://localhost:8545" gethHttpUrl == gethWsUrl
gethHttpsUrl == gethWsUrl
unspecifiedProtocolUrl == gethWsUrl
gethWsUrl == "ws://localhost:8545"