Skip to content
Snippets Groups Projects
Commit 1987a76b authored by inso's avatar inso
Browse files

Multiple bug fixing

- Managing lost communities (when no nodes could be joined)
- Fixing broadcast management
parent 8a24774a
No related branches found
No related tags found
No related merge requests found
...@@ -29,7 +29,8 @@ class Account(object): ...@@ -29,7 +29,8 @@ class Account(object):
be locally referenced by only one account. be locally referenced by only one account.
''' '''
def __init__(self, salt, pubkey, name, communities, wallets, contacts): def __init__(self, salt, pubkey, name, communities, wallets, contacts,
dead_communities):
''' '''
Constructor Constructor
''' '''
...@@ -37,6 +38,7 @@ class Account(object): ...@@ -37,6 +38,7 @@ class Account(object):
self.pubkey = pubkey self.pubkey = pubkey
self.name = name self.name = name
self.communities = communities self.communities = communities
self.dead_communities = dead_communities
self.wallets = wallets self.wallets = wallets
self.contacts = contacts self.contacts = contacts
...@@ -45,7 +47,7 @@ class Account(object): ...@@ -45,7 +47,7 @@ class Account(object):
''' '''
Constructor Constructor
''' '''
account = cls(None, None, name, communities, wallets, []) account = cls(None, None, name, communities, wallets, [], [])
return account return account
@classmethod @classmethod
...@@ -64,10 +66,15 @@ class Account(object): ...@@ -64,10 +66,15 @@ class Account(object):
wallets.append(Wallet.load(data)) wallets.append(Wallet.load(data))
communities = [] communities = []
dead_communities = []
for data in json_data['communities']: for data in json_data['communities']:
try:
communities.append(Community.load(data)) communities.append(Community.load(data))
except NoPeerAvailable:
dead_communities.append(data['currency'])
account = cls(salt, pubkey, name, communities, wallets, contacts) account = cls(salt, pubkey, name, communities, wallets,
contacts, dead_communities)
return account return account
def __eq__(self, other): def __eq__(self, other):
......
...@@ -62,19 +62,14 @@ class Community(object): ...@@ -62,19 +62,14 @@ class Community(object):
# After initializing the community from latest peers, # After initializing the community from latest peers,
# we refresh its peers tree # we refresh its peers tree
logging.debug("Creating community") logging.debug("Creating community")
try: found_peers = self.peering()
found_peers = self.peering() for p in found_peers:
for p in found_peers: if p.pubkey not in [peer.pubkey for peer in self.peers]:
if p.pubkey not in [peer.pubkey for peer in peers]: self.peers.append(p)
self.peers.append(p)
except NoPeerAvailable:
pass
logging.debug("{0} peers found".format(len(self.peers))) logging.debug("{0} peers found".format(len(self.peers)))
logging.debug([peer.pubkey for peer in peers])
try: self._cache.refresh()
self._cache.refresh()
except NoPeerAvailable:
pass
@classmethod @classmethod
def create(cls, currency, peer): def create(cls, currency, peer):
...@@ -209,26 +204,57 @@ class Community(object): ...@@ -209,26 +204,57 @@ class Community(object):
req = request(e.conn_handler(), **req_args) req = request(e.conn_handler(), **req_args)
try: try:
req.post(**post_args) req.post(**post_args)
return
except ValueError as e: except ValueError as e:
raise raise
except ConnectTimeout:
# Move the timeout peer to the end
self.peers.remove(peer)
self.peers.append(peer)
continue
except TimeoutError:
# Move the timeout peer to the end
self.peers.remove(peer)
self.peers.append(peer)
continue
except: except:
pass raise
return
raise NoPeerAvailable(self.currency, len(self.peers)) raise NoPeerAvailable(self.currency, len(self.peers))
def broadcast(self, request, req_args={}, post_args={}): def broadcast(self, request, req_args={}, post_args={}):
tries = 0
ok = False
value_error = None
for peer in self.peers: for peer in self.peers:
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)
logging.debug("Trying to connect to : " + peer.pubkey) logging.debug("Trying to connect to : " + peer.pubkey)
req = request(e.conn_handler(), **req_args) req = request(e.conn_handler(), **req_args)
try: try:
req.post(**post_args) req.post(**post_args)
ok = True
except ValueError as e: except ValueError as e:
if peer == self.peers[0]: value_error = e
raise continue
except ConnectTimeout:
tries = tries + 1
# Move the timeout peer to the end
self.peers.remove(peer)
self.peers.append(peer)
continue
except TimeoutError:
tries = tries + 1
# Move the timeout peer to the end
self.peers.remove(peer)
self.peers.append(peer)
continue
except: except:
pass raise
raise NoPeerAvailable(self.currency, len(self.peers))
if not ok:
raise value_error
if tries == len(self.peers):
raise NoPeerAvailable(self.currency, len(self.peers))
def jsonify_peers_list(self): def jsonify_peers_list(self):
data = [] data = []
......
...@@ -214,12 +214,10 @@ class Wallet(object): ...@@ -214,12 +214,10 @@ class Wallet(object):
inputs.append(s) inputs.append(s)
buf_inputs.remove(s) buf_inputs.remove(s)
if value >= amount: if value >= amount:
cache.available_sources = buf_inputs return (inputs, buf_inputs)
return inputs
raise NotEnoughMoneyError(value, community.currency, raise NotEnoughMoneyError(value, community.currency,
len(inputs), amount) len(inputs), amount)
return []
def tx_outputs(self, pubkey, amount, inputs): def tx_outputs(self, pubkey, amount, inputs):
outputs = [] outputs = []
...@@ -236,8 +234,12 @@ class Wallet(object): ...@@ -236,8 +234,12 @@ class Wallet(object):
def send_money(self, salt, password, community, def send_money(self, salt, password, community,
recipient, amount, message): recipient, amount, message):
inputs = self.tx_inputs(int(amount), community)
result = self.tx_inputs(int(amount), community)
inputs = result[0]
self.caches[community.currency].available_sources = result[1]
logging.debug("Inputs : {0}".format(inputs)) logging.debug("Inputs : {0}".format(inputs))
outputs = self.tx_outputs(recipient, amount, inputs) outputs = self.tx_outputs(recipient, amount, inputs)
logging.debug("Outputs : {0}".format(outputs)) logging.debug("Outputs : {0}".format(outputs))
tx = Transaction(PROTOCOL_VERSION, community.currency, tx = Transaction(PROTOCOL_VERSION, community.currency,
......
...@@ -157,6 +157,11 @@ class MainWindow(QMainWindow, Ui_MainWindow): ...@@ -157,6 +157,11 @@ class MainWindow(QMainWindow, Ui_MainWindow):
self.action_configure_parameters.setEnabled(False) self.action_configure_parameters.setEnabled(False)
self.action_set_as_default.setEnabled(False) self.action_set_as_default.setEnabled(False)
else: else:
for dead in self.app.current_account.dead_communities:
QMessageBox.critical(self, ":(",
"No {0} peers could be joined. Community was lost.".format(dead),
QMessageBox.Ok)
self.action_set_as_default.setEnabled(self.app.current_account.name self.action_set_as_default.setEnabled(self.app.current_account.name
!= self.app.default_account) != self.app.default_account)
self.password_asker = PasswordAskerDialog(self.app.current_account) self.password_asker = PasswordAskerDialog(self.app.current_account)
......
...@@ -70,18 +70,22 @@ class TransferMoneyDialog(QDialog, Ui_TransferMoneyDialog): ...@@ -70,18 +70,22 @@ class TransferMoneyDialog(QDialog, Ui_TransferMoneyDialog):
QMessageBox.critical(self, "Money transfer", QMessageBox.critical(self, "Money transfer",
"Something wrong happened : {0}".format(e), "Something wrong happened : {0}".format(e),
QMessageBox.Ok) QMessageBox.Ok)
return
except NotEnoughMoneyError as e: except NotEnoughMoneyError as e:
QMessageBox.critical(self, "Money transfer", QMessageBox.critical(self, "Money transfer",
"You don't have enough money available in this block : \n{0}" "You don't have enough money available in this block : \n{0}"
.format(e.message)) .format(e.message))
return
except NoPeerAvailable as e: except NoPeerAvailable as e:
QMessageBox.critical(self, "Money transfer", QMessageBox.critical(self, "Money transfer",
"Couldn't connect to network : {0}".format(e), "Couldn't connect to network : {0}".format(e),
QMessageBox.Ok) QMessageBox.Ok)
return
except Exception as e: except Exception as e:
QMessageBox.critical(self, "Error", QMessageBox.critical(self, "Error",
"{0}".format(e), "{0}".format(e),
QMessageBox.Ok) QMessageBox.Ok)
return
self.accepted.emit() self.accepted.emit()
self.close() self.close()
......
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