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):
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
......
......@@ -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):
......
......@@ -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.")
......
......@@ -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
......
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