-
Notifications
You must be signed in to change notification settings - Fork 79
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
Tx generic signing & Tx module (the first take) #12
Conversation
src/cosm/tx/tx_rest_client.py
Outdated
def Simulate(self, request: SimulateRequest) -> SimulateResponse: | ||
json_request = MessageToDict(request) | ||
response = self.rest_api.post( | ||
f"/cosmos/tx/v1beta1/simulate", |
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.
Define this as a constant.
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.
Use across (below)
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.
This URL path is changing, and in general it is different per each call.
Though, in this prticular case, following 3 calls below share the same URL path (note: it is still different than un this first call).
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.
I mean the /cosmos/tx/v1beta1/
part.
src/cosm/tests/tx/test_tx.py
Outdated
"FETCHD_GRPC_URL" not in os.environ, "Just for testing with local fetchd node" | ||
) | ||
def test_tx_broadcast(self): | ||
# from_pk = PrivateKey(bytes.fromhex("8bdfbd2eaad5dc4324d19fabed72882709dc080b39e61044d51b91a6e38f6871")) |
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.
Remove unwanted code (also below).
src/cosm/tests/tx/test_tx.py
Outdated
|
||
tx_body = TxBody() | ||
tx_body.memo = "very first tx" | ||
# tx_body.timeout_height = 0xffffffffffffffff |
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.
see above
src/cosm/tests/tx/test_tx.py
Outdated
|
||
from_pub_key_packed = Any() | ||
from_pub_key_packed.Pack(from_pub_key_pb) # , type_url_prefix="/") | ||
# signer_info.public_key = from_pub_key_packed |
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.
see above
src/cosm/tests/tx/test_tx.py
Outdated
fee=Fee(amount=[Coin(amount="0", denom="afet")], gas_limit=200000), | ||
) | ||
|
||
# sd = SignDoc() |
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.
see above
|
||
class RPCInterface(ABC): | ||
# Simulate simulates executing a transaction for estimating gas usage. | ||
def Simulate(self, request: svc.SimulateRequest) -> svc.SimulateResponse: |
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.
The best approach for abstract methods in python is to add @abstractmethod
above the method, add docstring for the methods and have nothing in the body (remove ...
).
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.
The ...
(or pass
) must be left there, otherwise the it is syntactic error when removed.
Regarding @abstractmethod
decorator, good catch, I just forgot to put the decorator on top of methods.
@@ -1,27 +1,30 @@ | |||
import urllib.error | |||
import urllib.request | |||
import requests |
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.
I see you reverted my changes.
import requests | |
import urllib.error | |
from urllib.request import Request, urlopen | |
class QueryRestClient: | |
def __init__(self, rest_address: str): | |
self.rest_address = rest_address | |
def get(self, request: str) -> str: | |
url = self.rest_address + request | |
try: | |
with urllib.request.urlopen(url) as f: | |
response = f.read().decode("utf-8") | |
return response | |
except urllib.error.HTTPError as e: | |
raise RuntimeError( | |
f"HTTPError when sending a get request.\n Request: {request}\n Response: {e.code}, {str(e.read().decode('utf-8'))})" | |
) | |
except urllib.error.URLError as e: | |
raise RuntimeError( | |
f"URLError when sending a get request.\n Request: {request}, Exception: {e})" | |
) | |
except Exception as e: | |
raise RuntimeError( | |
f"Exception during sending a get request.\n Request: {request}, Exception: {e})" | |
) | |
def post(self, url_path, json_request: dict) -> str: | |
try: | |
req = Request(self.rest_address + url_path) | |
req.add_header("Content-type", "application/json") | |
req.add_header("Accept", "application/json") | |
response = ( | |
urlopen(req, data=str(json_request).encode("utf-8")) | |
.read() | |
.decode("utf-8") | |
) | |
return response | |
except urllib.error.HTTPError as e: | |
raise RuntimeError( | |
f"HTTPError when sending a get request.\n Request: {url_path}, with json data: {json_request}\n Response: {e.code}, {str(e.read().decode('utf-8'))})" | |
) | |
except urllib.error.URLError as e: | |
raise RuntimeError( | |
f"URLError when sending a get request.\n Request: {url_path}, with json data: {json_request}, Exception: {e})" | |
) | |
except Exception as e: | |
raise RuntimeError( | |
f"Exception during sending a get request.\n Request: {url_path}, with json data: {json_request}, Exception: {e})" | |
) | |
No description provided.