Skip to content

Commit 311e6f2

Browse files
committed
Replace custom dev. version of mospy-wallet with the off. one from pypi. Delegate wait_transaction to wait_for_tx (from mospy). Rewritten utils.search_attribute after mospy update.
1 parent 8d8b2d9 commit 311e6f2

File tree

4 files changed

+30
-32
lines changed

4 files changed

+30
-32
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ dependencies = [
2525
"sentinel-protobuf==0.3.3",
2626
"grpcio>=1.51.1",
2727
"bip-utils==2.9.0",
28-
"mospy-wallet @ git+https://github.com/Tkd-Alex/mospy.git@development",
28+
"mospy-wallet==0.5.4",
2929
"pywgkey"
3030
]
3131

src/sentinel_sdk/transactor/transactor.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,21 +70,16 @@ def transaction(
7070
# tx_response = {"hash": hash, "code": code, "log": log}
7171
return tx_response
7272

73-
def wait_transaction(
74-
self, tx_hash: str, timeout: float = 120, pool_period: float = 10
75-
):
73+
def wait_for_tx(self, tx_hash: str, timeout: float = 120, poll_period: float = 10):
7674
if self._account is None or self._client is None:
7775
raise ValueError("Transactor was not initialized due missing secret, unable to wait transaction")
7876

79-
start = time.time()
80-
while 1:
81-
try:
82-
return self._client.get_tx(tx_hash)
83-
except grpc.RpcError as rpc_error:
84-
# https://github.com/grpc/grpc/tree/master/examples/python/errors
85-
# Instance of 'RpcError' has no 'code' member, work on runtime
86-
status_code = rpc_error.code() # pylint: disable=no-member
87-
if status_code == grpc.StatusCode.NOT_FOUND:
88-
if time.time() - start > timeout:
89-
return None
90-
time.sleep(pool_period)
77+
return self._client.wait_for_tx(
78+
tx_hash=tx_hash,
79+
timeout=timeout,
80+
poll_period=poll_period
81+
)
82+
83+
# back-compatibility
84+
def wait_transaction(self, tx_hash: str, timeout: float = 120, poll_period: float = 10):
85+
return self.wait_for_tx(tx_hash=tx_hash, timeout=timeout, poll_period=poll_period)

src/sentinel_sdk/utils.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import json
2-
from typing import Any
2+
import base64
33

44
# TODO: we want to move this method (?)
5-
def search_attribute(tx_response: Any, event_type: str, attribute_key: str) -> Any:
6-
for event in (tx_response.tx_response or tx_response).events:
7-
if event.type == event_type:
8-
for attribute in event.attributes:
9-
if attribute.key == attribute_key.encode():
10-
return json.loads(attribute.value)
5+
# In the latest version of mospy the response was converted as dict
6+
# from google.protobuf.json_format import MessageToDict
7+
# https://github.com/ctrl-Felix/mospy/blob/ea07705b6ec4c76cc355b7dc933fae7c09fd8429/src/mospy/clients/GRPCClient.py#L155
8+
def search_attribute(tx_response: dict, event_type: str, attribute_key: str) -> dict:
9+
for event in tx_response.get("txResponse", tx_response).get("events", []):
10+
if event["type"] == event_type:
11+
for attribute in event["attributes"]:
12+
if base64.b64decode(attribute["key"]) == attribute_key.encode():
13+
return json.loads(base64.b64decode(attribute["value"]).decode("utf-8"))
1114
return None

test/connect.sdk.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,19 @@
1111
# GRPC_VERBOSITY=debug GRPC_TRACE=tcp,http python test/queryall.py
1212

1313
# NEVER SHARE YOUR SECRET!
14-
secret = "abdc efgh 1234 5678"
14+
secret = input("your secret: ")
1515

1616
sdk = SDKInstance(grpcaddr="grpc.sentinel.co", grpcport=9090, secret=secret)
1717

1818
# The address can be founded in every modules because is inherited
1919
# For example: sdk.sessions._account.address
2020
print(sdk.nodes._account.address)
2121

22-
node_address = "sentnode1234abcd"
22+
node_address = input("give a node address: ")
2323
node = sdk.nodes.QueryNode(node_address)
2424
node_status = json.loads(sdk.nodes.QueryNodeStatus(node))
2525
assert node.address == node_address == node_status["result"]["address"]
2626

27-
2827
try:
2928
# tx = sdk.transactor.subscribe_to_gigabytes(node_address=node_address, gigabytes=1)
3029
tx = sdk.nodes.SubscribeToNode(
@@ -40,9 +39,10 @@
4039
print(rpc_error.details()) # pylint: disable=no-member
4140
print(rpc_error.debug_error_string()) # pylint: disable=no-member
4241

43-
# The wait_transaction can be founded in every modules because is inherited
44-
# For example: tx_response = sdk.sessions.wait_transaction(tx["hash"])
45-
tx_response = sdk.nodes.wait_transaction(tx["hash"])
42+
# The wait_for_tx can be founded in every modules because is inherited
43+
# For example: tx_response = sdk.sessions.wait_for_tx(tx["hash"])
44+
tx_response = sdk.nodes.wait_for_tx(tx["hash"])
45+
4646
subscription_id = search_attribute(
4747
tx_response, "sentinel.node.v2.EventCreateSubscription", "id"
4848
)
@@ -81,7 +81,7 @@
8181
# TODO: Currently doesn't work, we should call self.__client.load_account_data, before each tx
8282
try:
8383
tx = sdk.transactor.transaction(messages, tx_params=TxParams())
84-
tx_response = sdk.transactor.wait_transaction(tx["hash"])
84+
tx_response = sdk.transactor.wait_for_tx(tx["hash"])
8585
except grpc.RpcError as rpc_error:
8686
# TODO: this should be handled on skd side
8787
print(rpc_error.code()) # pylint: disable=no-member
@@ -92,10 +92,10 @@
9292
for session in sessions:
9393
if session.status == Status.ACTIVE.value:
9494
tx = sdk.sessions.EndSession(session_id=session.id, rating=0)
95-
print(sdk.sessions.wait_transaction(tx["hash"]))
95+
print(sdk.sessions.wait_for_tx(tx["hash"]))
9696

9797
tx = sdk.sessions.StartSession(subscription_id=int(subscription_id), address=node_address)
98-
tx_response = sdk.sessions.wait_transaction(tx["hash"])
98+
tx_response = sdk.sessions.wait_for_tx(tx["hash"])
9999

100100
session_id = search_attribute(
101101
tx_response, "sentinel.session.v2.EventStart", "id"

0 commit comments

Comments
 (0)