Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make json improvements #3496

Open
toppk opened this issue Sep 24, 2024 · 0 comments · May be fixed by #3498
Open

make json improvements #3496

toppk opened this issue Sep 24, 2024 · 0 comments · May be fixed by #3498

Comments

@toppk
Copy link

toppk commented Sep 24, 2024

What feature should we add?

i am debugging an issue, and I had to log the json for the best results. what was annoying, is that I couldn't just turn on websockets debugging with their WEBSOCKETS_MAX_LOG_SIZE env variable, because web3.py is sending binary so it's log messages , so I decided to take a look.

for reference this is what logging in websockets looks like when you send in bytes instead of a str.

websockets.client         DEBUG    > BINARY 7b 22 6a 73 6f 6e 72 70 63 22 3a 20 22 32 2e 30 ... 22 69 64 22 3a 20 30 7d [222 bytes]
websockets.client         DEBUG    < TEXT '{"jsonrpc":"2.0","id":"0","result":"0x10ff14c63...ae4c6e70bce401fd6f8c4"}' [104 bytes]

While looking around I found some room for improvement, happy to help if you think any of this is something you'd consider.

  1. have provider.socket_send take a string, and let async_ipc encode to bytes, WebSocketProvider can just pass the string to websockets, which will do it's own encoding, and log in text instead of hex bytes which is what it does if you send bytes.
  2. json.dumps should have the separartor=(',',':'). in _friendly_json_encode. no need to pad the network with 0x20's
  3. FriendlyJsonSerde should just be turned into @classmethod, since there's no instance state. save the churn of creating these objects over and over as encode_rpc_request does.
  4. since I think the point of FriendlyJsonSerde is to pinpoint failures if you happen to be encoding a dict like/list like object, shouldn't the encoder cls be passed to the recursive errors/encode calls?
  5. encode_batch_rpc_request . it's handrolling json (in binary, with spaces :) ) because we do the transformation of RPCEndpoint/params into rpc_dict and json encoding in one step. if that rpc_dict was separated out, and especially since FriendlyJsonSerde does this recursive error mapping, it seems like we could just give FriendlyJsonSerde.json_encode a list of rpc_dicts and it would be fine. and the had coded json '[' ']' can be removed.

if any of this stuff is of interest for this project, I'm happy to submit a PR.

@toppk toppk linked a pull request Sep 24, 2024 that will close this issue
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant