From 59e820397afdeb6f16a286577b87d1c214918224 Mon Sep 17 00:00:00 2001 From: Krzysztof Mochocki <kmochocki@syncad.com> Date: Fri, 11 Mar 2022 14:45:33 +0100 Subject: [PATCH] fixes in decoding incoming data --- hived/jsonsocket.py | 14 +++++++------- hived/test_ah_enum_virtual_ops.py | 8 ++++---- hived/test_ah_get_account_history.py | 8 ++++---- hived/test_ah_get_ops_in_block.py | 16 +++++----------- hived/test_ah_get_transaction.py | 8 ++++---- 5 files changed, 24 insertions(+), 30 deletions(-) diff --git a/hived/jsonsocket.py b/hived/jsonsocket.py index 0ce98f58..65a77eda 100644 --- a/hived/jsonsocket.py +++ b/hived/jsonsocket.py @@ -41,7 +41,7 @@ class JSONSocket(object): """ data - complete binary form of json request (ends with '\r\n' json - json request as python dict - + return value in form of json response as python dict """ if data == None: @@ -57,7 +57,7 @@ class JSONSocket(object): #if response == {}: # print("response is empty for request:", request.decode("utf-8")) return status, response - + def __call__(self, data=None, json=None): return self.request(data, json) @@ -79,14 +79,14 @@ class JSONSocket(object): return True, r else: return False, r - + return False, {} - + def __del__(self): if self.__sock: self.__sock.close() - + def hived_call(host, data=None, json=None, max_tries=10, timeout=0.1): """ host - [http[s]://<ip_address>:<port> @@ -97,7 +97,7 @@ def hived_call(host, data=None, json=None, max_tries=10, timeout=0.1): # except: # print("Cannot open socket for:", host) # return False, {} - + for i in range(max_tries): try: jsocket = JSONSocket(host, None, "/rpc", timeout) @@ -121,4 +121,4 @@ def hived_call(host, data=None, json=None, max_tries=10, timeout=0.1): def universal_call(url, data, *args, **kwargs): from requests import post result = post(url, json=data) - return [result.status_code, result.text] + return [result.status_code, result.content.decode('utf-8')] diff --git a/hived/test_ah_enum_virtual_ops.py b/hived/test_ah_enum_virtual_ops.py index 939518eb..ef220c29 100755 --- a/hived/test_ah_enum_virtual_ops.py +++ b/hived/test_ah_enum_virtual_ops.py @@ -9,9 +9,8 @@ import sys import json import os from argparse import ArgumentParser -from concurrent.futures import ThreadPoolExecutor +from concurrent.futures import Future, ThreadPoolExecutor from concurrent.futures import ProcessPoolExecutor -from concurrent.futures import wait from jsonsocket import universal_call as hived_call from pathlib import Path @@ -20,9 +19,10 @@ wdir = Path() errors = 0 -def future_end_cb(future): +def future_end_cb(future : Future): global errors - if future.result() == False: + exc = future.exception() + if exc is not None or future.result() == False: errors += 1 diff --git a/hived/test_ah_get_account_history.py b/hived/test_ah_get_account_history.py index f16e3314..83c61124 100755 --- a/hived/test_ah_get_account_history.py +++ b/hived/test_ah_get_account_history.py @@ -9,9 +9,8 @@ from argparse import ArgumentParser import sys import json import os -from concurrent.futures import ThreadPoolExecutor +from concurrent.futures import Future, ThreadPoolExecutor from concurrent.futures import ProcessPoolExecutor -from concurrent.futures import wait from jsonsocket import universal_call as hived_call from list_account import list_accounts from pathlib import Path @@ -21,9 +20,10 @@ wdir = Path() errors = 0 -def future_end_cb(future): +def future_end_cb(future : Future): global errors - if future.result() == False: + exc = future.exception() + if exc is not None or future.result() == False: errors += 1 diff --git a/hived/test_ah_get_ops_in_block.py b/hived/test_ah_get_ops_in_block.py index 17a24460..046fb7f7 100755 --- a/hived/test_ah_get_ops_in_block.py +++ b/hived/test_ah_get_ops_in_block.py @@ -8,15 +8,11 @@ from argparse import ArgumentParser import sys -import json +import simplejson as json import os -import shutil -from jsonsocket import JSONSocket from jsonsocket import universal_call as hived_call -from concurrent.futures import ThreadPoolExecutor +from concurrent.futures import Future, ThreadPoolExecutor from concurrent.futures import ProcessPoolExecutor -from concurrent.futures import Future -from concurrent.futures import wait from pathlib import Path @@ -24,9 +20,10 @@ wdir = Path() errors = 0 -def future_end_cb(future): +def future_end_cb(future : Future): global errors - if future.result() == False: + exc = future.exception() + if exc is not None or future.result() == False: errors += 1 @@ -139,9 +136,6 @@ def compare_results(f_block, l_block, url1, url2, max_tries=10, timeout=0.1): json1 = json.loads(json1) json2 = json.loads(json2) - #status1, json1 = hived_call(url1, data=request, max_tries=max_tries, timeout=timeout) - #status2, json2 = hived_call(url2, data=request, max_tries=max_tries, timeout=timeout) - if status1 == False or status2 == False or json1 != json2: print("Difference @block: {}\n".format(i)) diff --git a/hived/test_ah_get_transaction.py b/hived/test_ah_get_transaction.py index 8eb3638a..2f107c8d 100755 --- a/hived/test_ah_get_transaction.py +++ b/hived/test_ah_get_transaction.py @@ -9,9 +9,8 @@ import sys import json import os from argparse import ArgumentParser -from concurrent.futures import ThreadPoolExecutor +from concurrent.futures import Future, ThreadPoolExecutor from concurrent.futures import ProcessPoolExecutor -from concurrent.futures import wait from jsonsocket import universal_call as hived_call from pathlib import Path @@ -20,9 +19,10 @@ wdir = Path() errors = 0 -def future_end_cb(future): +def future_end_cb(future : Future): global errors - if future.result() == False: + exc = future.exception() + if exc is not None or future.result() == False: errors += 1 -- GitLab