Skip to content
Snippets Groups Projects
Commit 4ea042af authored by inso's avatar inso
Browse files

Fixed a bug when trying to add a community but canceling it

parent 131e528f
No related branches found
No related tags found
No related merge requests found
...@@ -87,16 +87,8 @@ class Account(object): ...@@ -87,16 +87,8 @@ class Account(object):
return True return True
return False return False
def add_community(self, server, port): def add_community(self, community):
logging.debug("Adding a 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) self.communities.append(community)
return community return community
......
...@@ -140,37 +140,41 @@ class Community(object): ...@@ -140,37 +140,41 @@ class Community(object):
e = next(e for e in peer.endpoints if type(e) is BMAEndpoint) e = next(e for e in peer.endpoints if type(e) is BMAEndpoint)
self._check_current_block(e) self._check_current_block(e)
try: try:
# We request the current block every five minutes # Do not cache block 0
# If a new block is mined we reset the cache if self.last_block["number"] != 0:
cache_key = (hash(request), cache_key = (hash(request),
hash(tuple(frozenset(sorted(req_args.keys())))), hash(tuple(frozenset(sorted(req_args.keys())))),
hash(tuple(frozenset(sorted(req_args.items())))), hash(tuple(frozenset(sorted(req_args.items())))),
hash(tuple(frozenset(sorted(get_args.keys())))), hash(tuple(frozenset(sorted(get_args.keys())))),
hash(tuple(frozenset(sorted(get_args.items()))))) hash(tuple(frozenset(sorted(get_args.items())))))
if cache_key not in self.requests_cache.keys(): if cache_key not in self.requests_cache.keys():
if e.server: if e.server:
logging.debug("Connecting to {0}:{1}".format(e.server, logging.debug("Connecting to {0}:{1}".format(e.server,
e.port)) e.port))
else: else:
logging.debug("Connecting to {0}:{1}".format(e.ipv4, logging.debug("Connecting to {0}:{1}".format(e.ipv4,
e.port)) 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) req = request(e.conn_handler(), **req_args)
data = req.get(**get_args) data = req.get(**get_args)
if inspect.isgenerator(data): return 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
except ValueError as e: except ValueError as e:
if '502' in str(e): if '502' in str(e):
continue continue
else: else:
raise raise
return self.requests_cache[cache_key]
raise NoPeerAvailable(self.currency) raise NoPeerAvailable(self.currency)
def request(self, request, req_args={}, get_args={}, cached=True): def request(self, request, req_args={}, get_args={}, cached=True):
......
...@@ -13,6 +13,7 @@ from PyQt5.QtWidgets import QDialog, QMenu, QMessageBox ...@@ -13,6 +13,7 @@ from PyQt5.QtWidgets import QDialog, QMenu, QMessageBox
from ..gen_resources.community_cfg_uic import Ui_CommunityConfigurationDialog from ..gen_resources.community_cfg_uic import Ui_CommunityConfigurationDialog
from ..models.peering import PeeringTreeModel from ..models.peering import PeeringTreeModel
from ..core.community import Community
from ..core.person import Person from ..core.person import Person
from ..tools.exceptions import PersonNotFoundError, NoPeerAvailable from ..tools.exceptions import PersonNotFoundError, NoPeerAvailable
...@@ -54,7 +55,12 @@ class StepPageInit(Step): ...@@ -54,7 +55,12 @@ class StepPageInit(Step):
account = self.config_dialog.account account = self.config_dialog.account
logging.debug("Account : {0}".format(account)) logging.debug("Account : {0}".format(account))
try: 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: except NoPeerAvailable:
QMessageBox.critical(self.config_dialog, "Server Error", QMessageBox.critical(self.config_dialog, "Server Error",
"Cannot join any peer in this community.") "Cannot join any peer in this community.")
......
...@@ -21,6 +21,9 @@ class WalletsListModel(QAbstractListModel): ...@@ -21,6 +21,9 @@ class WalletsListModel(QAbstractListModel):
super(WalletsListModel, self).__init__(parent) super(WalletsListModel, self).__init__(parent)
self.wallets = account.wallets self.wallets = account.wallets
self.community = community self.community = community
self.values = []
for w in self.wallets:
self.values.append(w.get_text(self.community))
def rowCount(self, parent): def rowCount(self, parent):
return len(self.wallets) return len(self.wallets)
...@@ -28,8 +31,7 @@ class WalletsListModel(QAbstractListModel): ...@@ -28,8 +31,7 @@ class WalletsListModel(QAbstractListModel):
def data(self, index, role): def data(self, index, role):
row = index.row() row = index.row()
if role == Qt.DisplayRole: if role == Qt.DisplayRole:
value = self.wallets[row].get_text(self.community) return self.values[row]
return value
elif role == Qt.EditRole: elif role == Qt.EditRole:
return self.wallets[row].name return self.wallets[row].name
...@@ -37,6 +39,7 @@ class WalletsListModel(QAbstractListModel): ...@@ -37,6 +39,7 @@ class WalletsListModel(QAbstractListModel):
if role == Qt.EditRole: if role == Qt.EditRole:
row = index.row() row = index.row()
self.wallets[row].name = value self.wallets[row].name = value
self.values[row] = self.wallets[row].get_text()
self.dataChanged.emit(index, index) self.dataChanged.emit(index, index)
return True return True
return False return False
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment