From e3c07562a40435272121cb31379d1fe7b94dca54 Mon Sep 17 00:00:00 2001 From: Tkd-Alex Date: Thu, 25 Jan 2024 14:36:56 +0100 Subject: [PATCH 1/3] Handle Unsubscription Errors and Process Accordingly, fix: https://github.com/MathNodes/meile-gui/issues/74 --- src/cli/wallet.py | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/src/cli/wallet.py b/src/cli/wallet.py index 1b7ca18..2a6f32a 100644 --- a/src/cli/wallet.py +++ b/src/cli/wallet.py @@ -4,6 +4,8 @@ import psutil import binascii import random +import grpc +import re from time import sleep from os import path, remove @@ -103,8 +105,8 @@ def subscribe(self, KEYNAME, NODE, DEPOSIT, GB, hourly): self.RPC = CONFIG['network'].get('rpc', HTTParams.RPC) self.GRPC = CONFIG['network'].get('grpc', HTTParams.GRPC) - grpc = self.GRPC.replace("grpc+http://", "").replace("/", "") # TODO: why const is grpc is saved as ... (?) - grpcaddr, grpcport = grpc.split(":") + grpc_url = self.GRPC.replace("grpc+http://", "").replace("/", "") # TODO: why const is grpc is saved as ... (?) + grpcaddr, grpcport = grpc_url.split(":") kr = self.__keyring(PASSWORD) private_key = kr.get_password("meile-gui", KEYNAME) # TODO: very ungly @@ -170,8 +172,8 @@ def unsubscribe(self, subId): self.GRPC = CONFIG['network'].get('grpc', HTTParams.GRPC) - grpc = self.GRPC.replace("grpc+http://", "").replace("/", "") # TODO: why const is grpc is saved as ... (?) - grpcaddr, grpcport = grpc.split(":") + grpc_url = self.GRPC.replace("grpc+http://", "").replace("/", "") # TODO: why const is grpc is saved as ... (?) + grpcaddr, grpcport = grpc_url.split(":") kr = self.__keyring(PASSWORD) private_key = kr.get_password("meile-gui", KEYNAME) # TODO: very ungly @@ -191,8 +193,25 @@ def unsubscribe(self, subId): # gas=ConfParams.GAS, gas_multiplier=ConfParams.GASADJUSTMENT ) - tx = sdk.subscriptions.Cancel(subId, tx_params=tx_params) tx_height = 0 + + try: + tx = sdk.subscriptions.Cancel(subId, tx_params=tx_params) + except grpc.RpcError as rpc_error: + details = rpc_error.details() # pylint: disable=no-member + # TODO: the following prints are only for debug + print("details", details) + print("code", rpc_error.code()) # pylint: disable=no-member + print("debug_error_string", rpc_error.debug_error_string()) # pylint: disable=no-member + + search = f"invalid status inactive_pending for subscription {subId}" + if re.search(search, details, re.IGNORECASE): + message = "Cannot unsubscribe. Pending session still on blockchain. Try your request again later." + else: + message = "Error connecting to gRPC server. Try your request again later." + + return {'hash' : None, 'success' : False, 'message' : message} + if tx.get("log", None) is None: tx_response = sdk.nodes.wait_transaction(tx["hash"]) tx_height = tx_response.tx_response.height From cd92ee2c14b061d392297318686b945cd9fb0e2a Mon Sep 17 00:00:00 2001 From: Tkd-Alex Date: Thu, 25 Jan 2024 14:39:08 +0100 Subject: [PATCH 2/3] Directly import RpcError from grpc --- src/cli/wallet.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cli/wallet.py b/src/cli/wallet.py index c12e743..88178bd 100644 --- a/src/cli/wallet.py +++ b/src/cli/wallet.py @@ -4,10 +4,10 @@ import psutil import binascii import random -import grpc import re from time import sleep from os import path, remove +from grpc import RpcError from json.decoder import JSONDecodeError @@ -210,7 +210,7 @@ def unsubscribe(self, subId): try: tx = sdk.subscriptions.Cancel(subId, tx_params=tx_params) - except grpc.RpcError as rpc_error: + except RpcError as rpc_error: details = rpc_error.details() # pylint: disable=no-member # TODO: the following prints are only for debug print("details", details) @@ -241,8 +241,8 @@ def connect(self, ID, address, type): self.GRPC = CONFIG['network'].get('grpc', HTTParams.GRPC) - grpc = self.GRPC.replace("grpc+http://", "").replace("/", "") # TODO: why const is grpc is saved as ... (?) - grpcaddr, grpcport = grpc.split(":") + grpc_url = self.GRPC.replace("grpc+http://", "").replace("/", "") # TODO: why const is grpc is saved as ... (?) + grpcaddr, grpcport = grpc_url.split(":") kr = self.__keyring(PASSWORD) private_key = kr.get_password("meile-gui", KEYNAME) # TODO: very ungly From 7795118b08e5daa9c261f33d0f6139b3df5d6046 Mon Sep 17 00:00:00 2001 From: Tkd-Alex Date: Fri, 2 Feb 2024 14:42:24 +0100 Subject: [PATCH 3/3] ulrparse instead of strip and replace for grpc --- src/cli/sentinel.py | 10 +++++----- src/cli/wallet.py | 14 +++++++------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/cli/sentinel.py b/src/cli/sentinel.py index 4065a44..1b5dd1d 100644 --- a/src/cli/sentinel.py +++ b/src/cli/sentinel.py @@ -7,7 +7,7 @@ from subprocess import Popen, PIPE, STDOUT from datetime import datetime,timedelta import time - +from urllib.parse import urlparse from treelib import Tree from geography.continents import OurWorld @@ -258,8 +258,8 @@ def get_subscriptions(self, ADDRESS): self.RPC = CONFIG['network'].get('rpc', HTTParams.RPC) self.GRPC = CONFIG['network'].get('grpc', HTTParams.GRPC) - grpc = self.GRPC.replace("grpc+http://", "").replace("/", "") # TODO: why const is grpc is saved as ... (?) - grpcaddr, grpcport = grpc.split(":") + + grpcaddr, grpcport = urlparse(self.GRPC).netloc.split(":") sdk = SDKInstance(grpcaddr, int(grpcport)) subscriptions = sdk.subscriptions.QuerySubscriptionsForAccount(ADDRESS, pagination=PageRequest(limit=1000)) @@ -337,8 +337,8 @@ def GetQuota(self, id): CONFIG = MeileConfig.read_configuration(MeileConfig.CONFFILE) self.GRPC = CONFIG['network'].get('grpc', HTTParams.GRPC) - grpc = self.GRPC.replace("grpc+http://", "").replace("/", "") # TODO: why const is grpc is saved as ... (?) - grpcaddr, grpcport = grpc.split(":") + + grpcaddr, grpcport = urlparse(self.GRPC).netloc.split(":") sdk = SDKInstance(grpcaddr, int(grpcport)) allocations = sdk.subscriptions.QueryAllocations(subscription_id=int(id)) diff --git a/src/cli/wallet.py b/src/cli/wallet.py index 44e6dc0..5287524 100644 --- a/src/cli/wallet.py +++ b/src/cli/wallet.py @@ -6,7 +6,7 @@ import random from time import sleep from os import path, remove - +from urllib.parse import urlparse from json.decoder import JSONDecodeError from conf.meile_config import MeileGuiConfig @@ -103,8 +103,8 @@ def subscribe(self, KEYNAME, NODE, DEPOSIT, GB, hourly): self.RPC = CONFIG['network'].get('rpc', HTTParams.RPC) self.GRPC = CONFIG['network'].get('grpc', HTTParams.GRPC) - grpc = self.GRPC.replace("grpc+http://", "").replace("/", "") # TODO: why const is grpc is saved as ... (?) - grpcaddr, grpcport = grpc.split(":") + + grpcaddr, grpcport = urlparse(self.GRPC).netloc.split(":") kr = self.__keyring(PASSWORD) private_key = kr.get_password("meile-gui", KEYNAME) # TODO: very ungly @@ -183,8 +183,8 @@ def unsubscribe(self, subId): self.GRPC = CONFIG['network'].get('grpc', HTTParams.GRPC) - grpc = self.GRPC.replace("grpc+http://", "").replace("/", "") # TODO: why const is grpc is saved as ... (?) - grpcaddr, grpcport = grpc.split(":") + + grpcaddr, grpcport = urlparse(self.GRPC).netloc.split(":") kr = self.__keyring(PASSWORD) private_key = kr.get_password("meile-gui", KEYNAME) # TODO: very ungly @@ -222,8 +222,8 @@ def connect(self, ID, address, type): self.GRPC = CONFIG['network'].get('grpc', HTTParams.GRPC) - grpc = self.GRPC.replace("grpc+http://", "").replace("/", "") # TODO: why const is grpc is saved as ... (?) - grpcaddr, grpcport = grpc.split(":") + + grpcaddr, grpcport = urlparse(self.GRPC).netloc.split(":") kr = self.__keyring(PASSWORD) private_key = kr.get_password("meile-gui", KEYNAME) # TODO: very ungly