status-im / nim-websock

Websocket for Nim
82 stars 15 forks source link

Websock fails to connect to alchemy wss api. #127

Closed KonradStaniec closed 2 years ago

KonradStaniec commented 2 years ago

Small repro:

import
  web3

let url = "wss://eth-goerli.g.alchemy.com/v2/api-key"
let w3 = waitFor newWeb3(url)

Instead of success it ends up with:

Error: unhandled exception: (RecvFatalAlert) Fatal alert (40) received from the peer (code: 296) [TLSStreamProtocolError]

Full stacktrace:

lc_proxy/play.nim(41)    play
vendor/nim-chronos/chronos/asyncloop.nim(1127) waitFor
vendor/nim-chronos/chronos/asyncloop.nim(288) poll
vendor/nim-chronos/chronos/asyncfutures2.nim(365) futureContinue
vendor/nim-chronos/chronos/asyncmacro2.nim(288) write
vendor/nim-chronos/chronos/asyncfutures2.nim(501) internalCheckComplete
[[reraised from:
lc_proxy/play.nim(41)    play
vendor/nim-chronos/chronos/asyncloop.nim(1127) waitFor
vendor/nim-chronos/chronos/asyncloop.nim(288) poll
vendor/nim-chronos/chronos/asyncfutures2.nim(365) futureContinue
vendor/nim-chronos/chronos/streams/asyncstream.nim(925) write
]]
[[reraised from:
lc_proxy/play.nim(41)    play
vendor/nim-chronos/chronos/asyncloop.nim(1127) waitFor
vendor/nim-chronos/chronos/asyncloop.nim(288) poll
vendor/nim-chronos/chronos/asyncfutures2.nim(365) futureContinue
vendor/nim-chronos/chronos/asyncmacro2.nim(131) write
]]
[[reraised from:
lc_proxy/play.nim(41)    play
vendor/nim-chronos/chronos/asyncloop.nim(1127) waitFor
vendor/nim-chronos/chronos/asyncloop.nim(288) poll
vendor/nim-chronos/chronos/asyncfutures2.nim(365) futureContinue
vendor/nim-chronos/chronos/asyncmacro2.nim(288) request
vendor/nim-chronos/chronos/asyncfutures2.nim(501) internalCheckComplete
]]
[[reraised from:
lc_proxy/play.nim(41)    play
vendor/nim-chronos/chronos/asyncloop.nim(1127) waitFor
vendor/nim-chronos/chronos/asyncloop.nim(288) poll
vendor/nim-chronos/chronos/asyncfutures2.nim(365) futureContinue
vendor/nim-chronos/chronos/asyncmacro2.nim(288) connect
vendor/nim-chronos/chronos/asyncfutures2.nim(501) internalCheckComplete
]]
[[reraised from:
lc_proxy/play.nim(41)    play
vendor/nim-chronos/chronos/asyncloop.nim(1127) waitFor
vendor/nim-chronos/chronos/asyncloop.nim(288) poll
vendor/nim-chronos/chronos/asyncfutures2.nim(365) futureContinue
vendor/nim-websock/websock/websock.nim(165) connect
]]
[[reraised from:
lc_proxy/play.nim(41)    play
vendor/nim-chronos/chronos/asyncloop.nim(1127) waitFor
vendor/nim-chronos/chronos/asyncloop.nim(288) poll
vendor/nim-chronos/chronos/asyncfutures2.nim(365) futureContinue
vendor/nim-chronos/chronos/asyncmacro2.nim(131) connect
]]
[[reraised from:
lc_proxy/play.nim(41)    play
vendor/nim-chronos/chronos/asyncloop.nim(1127) waitFor
vendor/nim-chronos/chronos/asyncloop.nim(288) poll
vendor/nim-chronos/chronos/asyncfutures2.nim(365) futureContinue
vendor/nim-chronos/chronos/asyncmacro2.nim(288) connect
vendor/nim-chronos/chronos/asyncfutures2.nim(501) internalCheckComplete
]]
[[reraised from:
lc_proxy/play.nim(41)    play
vendor/nim-chronos/chronos/asyncloop.nim(1127) waitFor
vendor/nim-chronos/chronos/asyncloop.nim(288) poll
vendor/nim-chronos/chronos/asyncfutures2.nim(365) futureContinue
vendor/nim-chronos/chronos/asyncmacro2.nim(288) newWeb3
vendor/nim-chronos/chronos/asyncfutures2.nim(501) internalCheckComplete
]]
[[reraised from:
lc_proxy/play.nim(41)    play
vendor/nim-chronos/chronos/asyncloop.nim(1129) waitFor
vendor/nim-chronos/chronos/asyncfutures2.nim(515) read
vendor/nim-chronos/chronos/asyncfutures2.nim(501) internalCheckComplete
]]

Ultimately what throws this initial exceptions is: https://github.com/status-im/nim-websock/blob/main/websock/http/client.nim#L108

When I tested this url with news or any other websocket client it worked correctly.

Menduist commented 2 years ago
import chronos, uri, chronos/apps/http/httpclient, stew/byteutils

let resp = waitFor HttpSessionRef.new().fetch(parseUri("https://eth-goerli.g.alchemy.com/v2/api-key"))
echo string.fromBytes(resp.data)

Works fine, so this is either coming from nim-web3, nim-websock or buggy server

etan-status commented 2 years ago

There are two issues:

  1. https://github.com/status-im/nim-websock/pull/128
  2. https://github.com/status-im/nim-chronos/issues/313

For (2), workaround is here: https://github.com/status-im/nim-json-rpc/pull/148

etan-status commented 2 years ago

Using the nim-json-rpc and nim-websock from linked issues above, the stacktrace now indicates missing API token instead:

/Users/etan/Documents/Repos/nimbus-eth2/test.nim(4) test
vendor/nim-chronos/chronos/asyncloop.nim(1127) waitFor
vendor/nim-chronos/chronos/asyncloop.nim(289) poll
vendor/nim-chronos/chronos/asyncfutures2.nim(365) futureContinue
vendor/nim-websock/websock/websock.nim(172) connect
[[reraised from:
/Users/etan/Documents/Repos/nimbus-eth2/test.nim(4) test
vendor/nim-chronos/chronos/asyncloop.nim(1127) waitFor
vendor/nim-chronos/chronos/asyncloop.nim(289) poll
vendor/nim-chronos/chronos/asyncfutures2.nim(365) futureContinue
vendor/nim-chronos/chronos/asyncmacro2.nim(288) connect
vendor/nim-chronos/chronos/asyncfutures2.nim(502) internalCheckComplete
]]
[[reraised from:
/Users/etan/Documents/Repos/nimbus-eth2/test.nim(4) test
vendor/nim-chronos/chronos/asyncloop.nim(1127) waitFor
vendor/nim-chronos/chronos/asyncloop.nim(289) poll
vendor/nim-chronos/chronos/asyncfutures2.nim(365) futureContinue
vendor/nim-chronos/chronos/asyncmacro2.nim(288) newWeb3
vendor/nim-chronos/chronos/asyncfutures2.nim(502) internalCheckComplete
]]
[[reraised from:
/Users/etan/Documents/Repos/nimbus-eth2/test.nim(4) test
vendor/nim-chronos/chronos/asyncloop.nim(1129) waitFor
vendor/nim-chronos/chronos/asyncfutures2.nim(515) read
vendor/nim-chronos/chronos/asyncfutures2.nim(502) internalCheckComplete
]]
Error: unhandled exception: Server did not reply with a websocket upgrade: Header code: 401 Header reason: Unauthorized Address: [2606:4700::6812:d3f]:443 [WSFailedUpgradeError]
KonradStaniec commented 2 years ago

yup, tested with my api token that both those fixes resolve situation.