Skip to content

Commit

Permalink
fix: parse for websocket connID (backport: evmos#1685) (#239) (evmos#273
Browse files Browse the repository at this point in the history
)

* fix parse for websocket connID

* update doc
  • Loading branch information
mmsqe committed Jun 14, 2023
1 parent 03441c3 commit 266040f
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Bug Fixes

- (rpc) [#1688](https://github.com/evmos/ethermint/pull/1688) Align filter rule for `debug_traceBlockByNumber`
* (rpc) [#1722](https://github.com/evmos/ethermint/pull/1722) Align revert response for `eth_estimateGas` and `eth_call` as Ethereum.
* (rpc) [#1720](https://github.com/evmos/ethermint/pull/1720) Fix next block fee for historical block and calculate base fee by params.
- (rpc) [#1722](https://github.com/evmos/ethermint/pull/1722) Align revert response for `eth_estimateGas` and `eth_call` as Ethereum.
- (rpc) [#1720](https://github.com/evmos/ethermint/pull/1720) Fix next block fee for historical block and calculate base fee by params.
- (rpc) [#1685](https://github.com/evmos/ethermint/pull/1685) Fix parse for websocket connID.

### Improvements

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
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)}"
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:
# allow retry
pass

loop.run_until_complete(async_test())


def test_batch_request_netversion(ethermint):
return

Expand Down

0 comments on commit 266040f

Please sign in to comment.