Skip to content

Commit

Permalink
Add support for HTTPS Web3 providers
Browse files Browse the repository at this point in the history
  • Loading branch information
zah committed Nov 23, 2021
1 parent 2d986c5 commit 88c623e
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 43 deletions.
10 changes: 10 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,13 @@
url = https://github.com/status-im/nim-ssz-serialization.git
ignore = untracked
branch = master
[submodule "vendor/nim-websock"]
path = vendor/nim-websock
url = https://github.com/status-im/nim-websock.git
ignore = untracked
branch = master
[submodule "vendor/nim-zlib"]
path = vendor/nim-zlib
url = https://github.com/status-im/nim-zlib.git
ignore = untracked
branch = master
40 changes: 6 additions & 34 deletions beacon_chain/eth1/eth1_monitor.nim
Original file line number Diff line number Diff line change
Expand Up @@ -243,43 +243,15 @@ template finalizedDepositsMerkleizer(m: Eth1Monitor): auto =
m.depositsChain.finalizedDepositsMerkleizer

proc fixupWeb3Urls*(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 {.dirty.} =
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://")):
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
if not (normalizedUrl.startsWith("https://") or
normalizedUrl.startsWith("http://") or
normalizedUrl.startsWith("wss://") or
normalizedUrl.startsWith("ws://")):
web3Url = "ws://" & web3Url
warn "The Web3 URL does not specify a protocol. Assuming a WebSocket server", web3Url
return

block gethRewrite:
web3Url = "ws://" & normalizedUrl.substr(pos)
warn "Only WebSocket web3 providers are supported. Rewriting URL", web3Url

template toGaugeValue(x: Quantity): int64 =
toGaugeValue(distinctBase x)

Expand Down
3 changes: 2 additions & 1 deletion beacon_chain/nimbus_beacon_node.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1340,7 +1340,8 @@ proc handleValidatorExitCommand(config: BeaconNodeConf) {.async.} =
let rpcClient = newRpcHttpClient()

try:
await connect(rpcClient, config.rpcUrlForExit.hostname, port)
await connect(rpcClient, config.rpcUrlForExit.hostname, port,
secure = config.rpcUrlForExit.scheme in ["https", "wss"])
except CatchableError as err:
fatal "Failed to connect to the beacon node RPC service", err = err.msg
quit 1
Expand Down
14 changes: 7 additions & 7 deletions tests/test_eth1_monitor.nim
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ suite "Eth1 monitor":

check:
mainnetWssUrl == "wss://mainnet.infura.io/ws/v3/6224f3c792cc443fafb64e70a98f871e"
mainnetHttpUrl == mainnetWssUrl
mainnetHttpsUrl == mainnetWssUrl
mainnetHttpUrl == "http://mainnet.infura.io/v3/6224f3c792cc443fafb64e70a98f871e"
mainnetHttpsUrl == "https://mainnet.infura.io/v3/6224f3c792cc443fafb64e70a98f871e"

goerliWssUrl == "wss://goerli.infura.io/ws/v3/6224f3c792cc443fafb64e70a98f871e"
goerliHttpUrl == goerliWssUrl
goerliHttpsUrl == goerliWssUrl
goerliHttpUrl == "http://goerli.infura.io/v3/6224f3c792cc443fafb64e70a98f871e"
goerliHttpsUrl == "https://goerli.infura.io/v3/6224f3c792cc443fafb64e70a98f871e"

gethHttpUrl == gethWsUrl
gethHttpsUrl == gethWsUrl
unspecifiedProtocolUrl == gethWsUrl
gethHttpUrl == "http://localhost:8545"
gethHttpsUrl == "https://localhost:8545"
unspecifiedProtocolUrl == "ws://localhost:8545"

gethWsUrl == "ws://localhost:8545"
1 change: 1 addition & 0 deletions vendor/nim-websock
Submodule nim-websock added at a697e3
1 change: 1 addition & 0 deletions vendor/nim-zlib
Submodule nim-zlib added at 6bbc67

0 comments on commit 88c623e

Please sign in to comment.