diff --git a/src/cutecoin/core/account.py b/src/cutecoin/core/account.py index 174ebcb777f9cb50e25f91cd2a1b334f5e6ac1ce..4fc001be71c80c95a02cca77d5bf0dec242e5af0 100644 --- a/src/cutecoin/core/account.py +++ b/src/cutecoin/core/account.py @@ -87,16 +87,8 @@ class Account(object): return True return False - def add_community(self, server, port): + def add_community(self, community): logging.debug("Adding a community") - - peering = bma.network.Peering(ConnectionHandler(server, port)) - peer_data = peering.get() - peer = Peer.from_signed_raw("{0}{1}\n".format(peer_data['raw'], - peer_data['signature'])) - currency = peer.currency - - community = Community.create(currency, peer) self.communities.append(community) return community diff --git a/src/cutecoin/core/community.py b/src/cutecoin/core/community.py index 4b831ac1ceba67e19841de7231966ce622d60b48..0f3cf086094fc8b850811b04c08937d25e1b8257 100644 --- a/src/cutecoin/core/community.py +++ b/src/cutecoin/core/community.py @@ -140,37 +140,41 @@ class Community(object): e = next(e for e in peer.endpoints if type(e) is BMAEndpoint) self._check_current_block(e) try: - # We request the current block every five minutes - # If a new block is mined we reset the cache - cache_key = (hash(request), - hash(tuple(frozenset(sorted(req_args.keys())))), - hash(tuple(frozenset(sorted(req_args.items())))), - hash(tuple(frozenset(sorted(get_args.keys())))), - hash(tuple(frozenset(sorted(get_args.items()))))) - - if cache_key not in self.requests_cache.keys(): - if e.server: - logging.debug("Connecting to {0}:{1}".format(e.server, - e.port)) - else: - logging.debug("Connecting to {0}:{1}".format(e.ipv4, - e.port)) - + # Do not cache block 0 + if self.last_block["number"] != 0: + cache_key = (hash(request), + hash(tuple(frozenset(sorted(req_args.keys())))), + hash(tuple(frozenset(sorted(req_args.items())))), + hash(tuple(frozenset(sorted(get_args.keys())))), + hash(tuple(frozenset(sorted(get_args.items()))))) + + if cache_key not in self.requests_cache.keys(): + if e.server: + logging.debug("Connecting to {0}:{1}".format(e.server, + e.port)) + else: + logging.debug("Connecting to {0}:{1}".format(e.ipv4, + e.port)) + + req = request(e.conn_handler(), **req_args) + data = req.get(**get_args) + if inspect.isgenerator(data): + cached_data = [] + for d in data: + cached_data.append(d) + self.requests_cache[cache_key] = cached_data + else: + self.requests_cache[cache_key] = data + return self.requests_cache[cache_key] + else: req = request(e.conn_handler(), **req_args) data = req.get(**get_args) - if inspect.isgenerator(data): - cached_data = [] - for d in data: - cached_data.append(d) - self.requests_cache[cache_key] = cached_data - else: - self.requests_cache[cache_key] = data + return data except ValueError as e: if '502' in str(e): continue else: raise - return self.requests_cache[cache_key] raise NoPeerAvailable(self.currency) def request(self, request, req_args={}, get_args={}, cached=True): diff --git a/src/cutecoin/gui/process_cfg_community.py b/src/cutecoin/gui/process_cfg_community.py index b273a4c6bfac757da0c86f20aa9e5948204fd60b..2b257a5f38869871de2580f9259f46af81c5adf9 100644 --- a/src/cutecoin/gui/process_cfg_community.py +++ b/src/cutecoin/gui/process_cfg_community.py @@ -13,6 +13,7 @@ from PyQt5.QtWidgets import QDialog, QMenu, QMessageBox from ..gen_resources.community_cfg_uic import Ui_CommunityConfigurationDialog from ..models.peering import PeeringTreeModel +from ..core.community import Community from ..core.person import Person from ..tools.exceptions import PersonNotFoundError, NoPeerAvailable @@ -54,7 +55,12 @@ class StepPageInit(Step): account = self.config_dialog.account logging.debug("Account : {0}".format(account)) try: - self.config_dialog.community = account.add_community(server, port) + peering = bma.network.Peering(ConnectionHandler(server, port)) + peer_data = peering.get() + peer = Peer.from_signed_raw("{0}{1}\n".format(peer_data['raw'], + peer_data['signature'])) + currency = peer.currency + self.config_dialog.community = Community.create(currency, peer) except NoPeerAvailable: QMessageBox.critical(self.config_dialog, "Server Error", "Cannot join any peer in this community.") diff --git a/src/cutecoin/models/wallets.py b/src/cutecoin/models/wallets.py index a2f9dabb1a8ecaba136b1eb8c566d6e188052ea4..b745a6e8b4023d1be4dda54903943c36aaf3f9bd 100644 --- a/src/cutecoin/models/wallets.py +++ b/src/cutecoin/models/wallets.py @@ -21,6 +21,9 @@ class WalletsListModel(QAbstractListModel): super(WalletsListModel, self).__init__(parent) self.wallets = account.wallets self.community = community + self.values = [] + for w in self.wallets: + self.values.append(w.get_text(self.community)) def rowCount(self, parent): return len(self.wallets) @@ -28,8 +31,7 @@ class WalletsListModel(QAbstractListModel): def data(self, index, role): row = index.row() if role == Qt.DisplayRole: - value = self.wallets[row].get_text(self.community) - return value + return self.values[row] elif role == Qt.EditRole: return self.wallets[row].name @@ -37,6 +39,7 @@ class WalletsListModel(QAbstractListModel): if role == Qt.EditRole: row = index.row() self.wallets[row].name = value + self.values[row] = self.wallets[row].get_text() self.dataChanged.emit(index, index) return True return False