Skip to content
Snippets Groups Projects
Commit 683c8af3 authored by inso's avatar inso
Browse files

Handling disconnected state

parent 6e842318
No related branches found
No related tags found
No related merge requests found
...@@ -87,7 +87,9 @@ class Cache(): ...@@ -87,7 +87,9 @@ class Cache():
if cache_key not in self.data.keys(): if cache_key not in self.data.keys():
result = self.community.request(request, req_args, get_args, result = self.community.request(request, req_args, get_args,
cached=False) cached=False)
# For block 0, we should have a different behaviour
# Community members and certifications
# Should be requested without caching
self.data[cache_key] = result self.data[cache_key] = result
return self.data[cache_key] return self.data[cache_key]
else: else:
...@@ -254,11 +256,16 @@ class Community(QObject): ...@@ -254,11 +256,16 @@ class Community(QObject):
:return: The monetary mass value :return: The monetary mass value
''' '''
try: try:
block = self.request(bma.blockchain.Current) # Get cached block by block number
block_number = self.network.latest_block
block = self.request(bma.blockchain.Block,
req_args={'number': block_number})
return block['monetaryMass'] return block['monetaryMass']
except ValueError as e: except ValueError as e:
if '404' in e: if '404' in e:
return 0 return 0
except NoPeerAvailable as e:
return 0
@property @property
def nb_members(self): def nb_members(self):
...@@ -268,11 +275,16 @@ class Community(QObject): ...@@ -268,11 +275,16 @@ class Community(QObject):
:return: The community members number :return: The community members number
''' '''
try: try:
block = self.request(bma.blockchain.Current) # Get cached block by block number
block_number = self.network.latest_block
block = self.request(bma.blockchain.Block,
req_args={'number': block_number})
return block['membersCount'] return block['membersCount']
except ValueError as e: except ValueError as e:
if '404' in e: if '404' in e:
return 0 return 0
except NoPeerAvailable as e:
return 0
@property @property
def network(self): def network(self):
...@@ -369,7 +381,6 @@ class Community(QObject): ...@@ -369,7 +381,6 @@ class Community(QObject):
''' '''
Start the refresh processing of the cache Start the refresh processing of the cache
''' '''
# We have to refresh node before refresh cache
self._cache.refresh() self._cache.refresh()
def request(self, request, req_args={}, get_args={}, cached=True): def request(self, request, req_args={}, get_args={}, cached=True):
......
...@@ -12,7 +12,8 @@ from ucoinpy import PROTOCOL_VERSION ...@@ -12,7 +12,8 @@ from ucoinpy import PROTOCOL_VERSION
from ucoinpy.documents.certification import SelfCertification from ucoinpy.documents.certification import SelfCertification
from ucoinpy.documents.membership import Membership from ucoinpy.documents.membership import Membership
from ..tools.exceptions import Error, PersonNotFoundError,\ from ..tools.exceptions import Error, PersonNotFoundError,\
MembershipNotFoundError MembershipNotFoundError, \
NoPeerAvailable
from PyQt5.QtCore import QMutex from PyQt5.QtCore import QMutex
...@@ -51,6 +52,7 @@ class cached(object): ...@@ -51,6 +52,7 @@ class cached(object):
except KeyError: except KeyError:
value = self.func(inst, community) value = self.func(inst, community)
inst._cache[community.currency][self.func.__name__] = value inst._cache[community.currency][self.func.__name__] = value
finally: finally:
inst._cache_mutex.unlock() inst._cache_mutex.unlock()
...@@ -219,6 +221,9 @@ class Person(object): ...@@ -219,6 +221,9 @@ class Person(object):
except ValueError as e: except ValueError as e:
if '400' in str(e): if '400' in str(e):
raise MembershipNotFoundError(self.pubkey, community.name) raise MembershipNotFoundError(self.pubkey, community.name)
except Exception as e:
logging.debug('bma.blockchain.Membership request error : ' + str(e))
raise MembershipNotFoundError(self.pubkey, community.name)
#TODO: Manage 'OUT' memberships ? Maybe ? #TODO: Manage 'OUT' memberships ? Maybe ?
@cached @cached
...@@ -247,6 +252,9 @@ class Person(object): ...@@ -247,6 +252,9 @@ class Person(object):
except ValueError as e: except ValueError as e:
if '400' in str(e): if '400' in str(e):
raise MembershipNotFoundError(self.pubkey, community.name) raise MembershipNotFoundError(self.pubkey, community.name)
except Exception as e:
logging.debug('bma.blockchain.Membership request error : ' + str(e))
raise MembershipNotFoundError(self.pubkey, community.name)
return membership_data return membership_data
...@@ -263,6 +271,9 @@ class Person(object): ...@@ -263,6 +271,9 @@ class Person(object):
return certifiers['isMember'] return certifiers['isMember']
except ValueError: except ValueError:
return False return False
except Exception as e:
logging.debug('bma.wot.CertifiersOf request error : ' + str(e))
return False
@cached @cached
def certifiers_of(self, community): def certifiers_of(self, community):
......
...@@ -258,7 +258,6 @@ class MainWindow(QMainWindow, Ui_MainWindow): ...@@ -258,7 +258,6 @@ class MainWindow(QMainWindow, Ui_MainWindow):
self.currencies_tabwidget.clear() self.currencies_tabwidget.clear()
if self.app.current_account: if self.app.current_account:
for community in self.app.current_account.communities: for community in self.app.current_account.communities:
try:
tab_currency = CurrencyTabWidget(self.app, community, tab_currency = CurrencyTabWidget(self.app, community,
self.password_asker, self.password_asker,
self.status_label) self.status_label)
...@@ -266,15 +265,6 @@ class MainWindow(QMainWindow, Ui_MainWindow): ...@@ -266,15 +265,6 @@ class MainWindow(QMainWindow, Ui_MainWindow):
self.currencies_tabwidget.addTab(tab_currency, self.currencies_tabwidget.addTab(tab_currency,
QIcon(":/icons/currency_icon"), QIcon(":/icons/currency_icon"),
community.name) community.name)
except NoPeerAvailable as e:
QMessageBox.critical(self, "Could not join {0}".format(community.currency),
str(e),
QMessageBox.Ok)
continue
except requests.exceptions.RequestException as e:
QMessageBox.critical(self, ":(",
str(e),
QMessageBox.Ok)
def refresh_accounts(self): def refresh_accounts(self):
self.menu_change_account.clear() self.menu_change_account.clear()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment