mirror of
https://github.com/status-im/nimbus-eth2.git
synced 2025-01-09 13:56:23 +00:00
Fix #1784; Handle Infura HTTPS URLs
This commit is contained in:
parent
f596a24c4c
commit
a8a66fe3f6
@ -90,6 +90,38 @@ 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) =
|
||||||
|
## 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
|
# 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
|
||||||
# doAssert SECONDS_PER_ETH1_BLOCK * preset.ETH1_FOLLOW_DISTANCE < GENESIS_DELAY,
|
# doAssert SECONDS_PER_ETH1_BLOCK * preset.ETH1_FOLLOW_DISTANCE < GENESIS_DELAY,
|
||||||
@ -357,6 +389,9 @@ proc init*(T: type Eth1Monitor,
|
|||||||
web3Url: string,
|
web3Url: string,
|
||||||
depositContractAddress: Eth1Address,
|
depositContractAddress: Eth1Address,
|
||||||
depositContractDeployedAt: string): Future[Result[T, string]] {.async.} =
|
depositContractDeployedAt: string): Future[Result[T, string]] {.async.} =
|
||||||
|
var web3Url = web3Url
|
||||||
|
fixupInfuraUrls web3Url
|
||||||
|
|
||||||
let web3 = try: await newWeb3(web3Url)
|
let web3 = try: await newWeb3(web3Url)
|
||||||
except CatchableError as err:
|
except CatchableError as err:
|
||||||
debugEcho err.msg
|
debugEcho err.msg
|
||||||
|
@ -8,5 +8,33 @@ import
|
|||||||
suite "Eth1 Chain":
|
suite "Eth1 Chain":
|
||||||
discard
|
discard
|
||||||
|
|
||||||
suite "Mainchain monitor":
|
suite "Eth1 monitor":
|
||||||
discard
|
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"
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user