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

feat(mocknet): change HTTP request logic #10023

Merged
merged 3 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pytest/tests/mocknet/helpers/neard_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@ def serve(self, port):
# something so lightweight
main_loop = threading.Thread(target=self.main_loop)
main_loop.start()
s = RpcServer(('0.0.0.0', port), self)
s = RpcServer(('localhost', port), self)
marcelo-gonzalez marked this conversation as resolved.
Show resolved Hide resolved
s.serve_forever()


Expand Down
22 changes: 15 additions & 7 deletions pytest/tests/mocknet/mirror.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""
from argparse import ArgumentParser, BooleanOptionalAction
import pathlib
import json
import random
from rc import pmap, run
import requests
Expand Down Expand Up @@ -328,14 +329,21 @@ def stop_traffic_cmd(args, traffic_generator, nodes):


def neard_runner_jsonrpc(node, method, params=[]):
j = {'method': method, 'params': params, 'id': 'dontcare', 'jsonrpc': '2.0'}
r = requests.post(f'http://{node.machine.ip}:3000', json=j, timeout=30)
if r.status_code != 200:
logger.warning(
f'bad response {r.status_code} trying to send {method} JSON RPC to neard runner on {node.instance_name}:\n{r.content}'
body = {
'method': method,
'params': params,
'id': 'dontcare',
'jsonrpc': '2.0'
}
body = json.dumps(body)
r = run_cmd(node, f'curl localhost:3000 -d \'{body}\'')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if that’s important? but here if method or params contain a quote, then this will fail. Might need to replace all ' in body with '"'"' first

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would feel slightly better with some escaping provided by a library but as long as this works it's ok.

response = json.loads(r.stdout)
if 'error' in response:
# TODO: errors should be handled better here in general but just exit for now
sys.exit(
f'bad response trying to send {method} JSON RPC to neard runner on {node.instance_name}:\n{response}'
)
r.raise_for_status()
return r.json()['result']
return response['result']


def neard_runner_start(node):
Expand Down