Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

fix: parse for websocket connID #1685

Closed
wants to merge 13 commits into from
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ Ref: https://keepachangelog.com/en/1.0.0/

* (deps) [#1168](https://github.com/evmos/ethermint/pull/1168) Upgrade Cosmos SDK to [`v0.46.6`]

### Bug Fixes

* (rpc) [#1685](https://github.com/evmos/ethermint/pull/1685) Fix parse for websocket connID.

## [v0.21.0] - 2023-01-26

### State Machine Breaking
Expand Down
16 changes: 8 additions & 8 deletions gomod2nix.toml
Original file line number Diff line number Diff line change
Expand Up @@ -490,23 +490,23 @@ schema = 3
version = "v0.0.0-20220722155223-a9213eeb770e"
hash = "sha256-kNgzydWRpjm0sZl4uXEs3LX5L0xjJtJRAFf/CTlYUN4="
[mod."golang.org/x/net"]
version = "v0.5.0"
hash = "sha256-HpbIAiLs7S1+tVsaSSdbCPw1IK43A0bFFuSzPSyjLbo="
version = "v0.6.0"
hash = "sha256-e8F4kMogxT392mmimvcAmkUtD/5vcQRPWDwH238m8FU="
[mod."golang.org/x/oauth2"]
version = "v0.0.0-20221014153046-6fdb5e3db783"
hash = "sha256-IoygidVNqyAZmN+3macDeIefK8hhJToygpcqlwehdYQ="
[mod."golang.org/x/sync"]
version = "v0.1.0"
hash = "sha256-Hygjq9euZ0qz6TvHYQwOZEjNiTbTh1nSLRAWZ6KFGR8="
[mod."golang.org/x/sys"]
version = "v0.4.0"
hash = "sha256-jchMzHCH5dg+IL/F+LqaX/fyAcB/nvHQpfBjqwaRJH0="
version = "v0.5.0"
hash = "sha256-0LTr3KeJ1OMQAwYUQo1513dXJtQAJn5Dq8sFkc8ps1U="
[mod."golang.org/x/term"]
version = "v0.4.0"
hash = "sha256-wQKxHV10TU4vCU8Re2/hFmAbur/jRWEOB8QXBzgTFNY="
version = "v0.5.0"
hash = "sha256-f3DiX7NkDsEZpPS+PbmnOH9F5WHFZ1sQrfFg/T2UPno="
[mod."golang.org/x/text"]
version = "v0.6.0"
hash = "sha256-+bpeRWR3relKACdal6NPj+eP5dnWCplTViArSN7/qA4="
version = "v0.7.0"
hash = "sha256-ydgUqX+t5Qke16C6d3FP/06U/N1n+rUKpLRFj4KXjwk="
[mod."golang.org/x/xerrors"]
version = "v0.0.0-20220907171357-04be3eba64a2"
hash = "sha256-6+zueutgefIYmgXinOflz8qGDDDj0Zhv+2OkGhBTKno="
Expand Down
13 changes: 11 additions & 2 deletions rpc/websockets.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"math/big"
"net"
"net/http"
"strconv"
"sync"

"github.com/cosmos/cosmos-sdk/client"
Expand Down Expand Up @@ -225,8 +226,16 @@ func (s *websocketsServer) readLoop(wsConn *wsConn) {
continue
}

connID, ok := msg["id"].(float64)
if !ok {
var connID float64
switch id := msg["id"].(type) {
case string:
connID, err = strconv.ParseFloat(id, 64)
case float64:
connID = id
default:
err = fmt.Errorf("unknown type")
}
if err != nil {
s.sendErrResponse(
wsConn,
fmt.Errorf("invalid type for connection ID: %T", msg["id"]).Error(),
Expand Down
12 changes: 12 additions & 0 deletions tests/integration_tests/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/integration_tests/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ websockets = "^10.3"
toml = "^0.10.2"
pysha3 = "^1.0.2"
jsonnet = "^0.18.0"
flaky = "^3.7.0"

[tool.poetry.dev-dependencies]

Expand Down
1 change: 1 addition & 0 deletions tests/integration_tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ def test_event_log_filter_by_address(cluster):
assert len(flt2.get_new_entries()) == 0


@pytest.mark.flaky
def test_event_log_filter_by_topic(cluster):
w3: Web3 = cluster.w3

Expand Down
58 changes: 58 additions & 0 deletions tests/integration_tests/test_websockets.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import asyncio
import json
from collections import defaultdict

import websockets
from pystarport import ports


def test_single_request_netversion(ethermint):
ethermint.use_websocket()
eth_ws = ethermint.w3.provider
Expand All @@ -7,6 +15,7 @@ def test_single_request_netversion(ethermint):
# net_version should be 9000
assert response["result"] == "9000", "got " + response["result"] + ", expected 9000"


# note:
# batch requests still not implemented in web3.py
# todo: follow https://github.com/ethereum/web3.py/issues/832, add tests when complete
Expand All @@ -15,6 +24,55 @@ def test_single_request_netversion(ethermint):
# todo: follow https://github.com/ethereum/web3.py/issues/1402, add tests when complete


class Client:
def __init__(self, ws):
self._ws = ws
self._subs = defaultdict(asyncio.Queue)
self._rsps = defaultdict(asyncio.Queue)

async def receive_loop(self):
while True:
msg = json.loads(await self._ws.recv())
if "id" in msg:
# responses
await self._rsps[msg["id"]].put(msg)

async def recv_response(self, rpcid):
rsp = await self._rsps[rpcid].get()
del self._rsps[rpcid]
return rsp

async def send(self, id):
await self._ws.send(
json.dumps({"id": id, "method": "web3_clientVersion", "params": []})
)
rsp = await self.recv_response(id)
assert "error" not in rsp


def test_web3_client_version(ethermint):
ethermint_ws = ethermint.copy()
ethermint_ws.use_websocket()
port = ethermint_ws.base_port(0)
url = f"ws://127.0.0.1:{ports.evmrpc_ws_port(port)}"
mmsqe marked this conversation as resolved.
Show resolved Hide resolved
loop = asyncio.get_event_loop()

async def async_test():
async with websockets.connect(url) as ws:
c = Client(ws)
t = asyncio.create_task(c.receive_loop())
# run send concurrently
await asyncio.gather(*[c.send(id) for id in ["0", 1, 2.0]])
t.cancel()
try:
await t
except asyncio.CancelledError:
Fixed Show fixed Hide fixed
# allow retry
pass

loop.run_until_complete(async_test())


def test_batch_request_netversion(ethermint):
return

Expand Down