Repository navigation
fix(jsonrpc): serialize every call in a batch #123
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
Open
zakazaka95
wants to merge
1
commit into
chainstacklabs:main
Choose a base branch
from
zakazaka95:fix/jsonrpc-complete-batches
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import json | ||
| import unittest | ||
|
|
||
| from chainbench.util.jsonrpc import RpcCall, generate_batch_request_body | ||
|
|
||
|
|
||
| class JsonRpcBatchTests(unittest.TestCase): | ||
| def test_single_call_is_included(self) -> None: | ||
| result = json.loads(generate_batch_request_body([RpcCall("eth_blockNumber")])) | ||
|
|
||
| self.assertEqual(result, [{"jsonrpc": "2.0", "method": "eth_blockNumber", "params": [], "id": 0}]) | ||
|
|
||
| def test_all_calls_preserve_order_params_and_positional_ids(self) -> None: | ||
| calls = [ | ||
| RpcCall("eth_blockNumber"), | ||
| RpcCall("eth_getBalance", ["0x123", "latest"]), | ||
| RpcCall("getBlock", [42, {"encoding": "json"}]), | ||
| ] | ||
|
|
||
| result = json.loads(generate_batch_request_body(calls)) | ||
|
|
||
| self.assertEqual(len(result), len(calls)) | ||
| self.assertEqual([entry["method"] for entry in result], [call.method for call in calls]) | ||
| self.assertEqual([entry["params"] for entry in result], [[], ["0x123", "latest"], [42, {"encoding": "json"}]]) | ||
| self.assertEqual([entry["id"] for entry in result], [0, 1, 2]) | ||
|
|
||
| def test_repeated_call_instances_each_get_a_distinct_id(self) -> None: | ||
| call = RpcCall("eth_chainId", request_id=99) | ||
|
|
||
| result = json.loads(generate_batch_request_body([call, call, call])) | ||
|
|
||
| self.assertEqual([entry["method"] for entry in result], ["eth_chainId"] * 3) | ||
| self.assertEqual([entry["id"] for entry in result], [0, 1, 2]) | ||
|
|
||
| def test_explicit_zero_overrides_an_existing_request_id(self) -> None: | ||
| call = RpcCall("eth_chainId", request_id=99) | ||
|
|
||
| self.assertEqual(call.request_body(0)["id"], 0) | ||
|
|
||
| def test_empty_batch_remains_empty(self) -> None: | ||
| self.assertEqual(json.loads(generate_batch_request_body([])), []) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script executed:
Repository: chainstacklabs/chainbench
Length of output: 10939
Script executed:
Repository: chainstacklabs/chainbench
Length of output: 32473
Web query:
JSON-RPC 2.0 specification named parameters object by-name method subtract exampleResult:
Script executed:
Repository: chainstacklabs/chainbench
Length of output: 32473
Script executed:
Repository: chainstacklabs/chainbench
Length of output: 24220
Script executed:
Repository: chainstacklabs/chainbench
Length of output: 11296
Web query:
site:jsonrpc.org/specification "rpc call with named parameters" "subtract" "minuend" "subtrahend" resultResult:
Preserve object-valued JSON-RPC parameters.
When a batch contains
RpcCall("starknet_call", params_dict),RpcCall.request_bodychanges the named-parameter object to a one-element array. JSON-RPC 2.0 requires an object for by-name parameters, so the endpoint can reject the call. Keep dictionaries unchanged and wrap only positional parameters at the call site.Suggested fix
ast-grep (0.51.1)
[info] 40-40: use jsonify instead of json.dumps for JSON output
Context: json.dumps([rpc_call.request_body(i) for i, rpc_call in enumerate(rpc_calls)])
Note: [CWE-116] Improper Encoding or Escaping of Output.
(use-jsonify)