diff --git a/src/cutecoin/core/community.py b/src/cutecoin/core/community.py index 0e8b0c163de23175024661b82c323bc9356bab13..a930916abdb8eea45e827d35e6a42142d53eb68a 100644 --- a/src/cutecoin/core/community.py +++ b/src/cutecoin/core/community.py @@ -19,7 +19,7 @@ from requests.exceptions import RequestException class Cache(): - _saved_requests = [str(bma.blockchain.Block)] + _saved_requests = [str(bma.blockchain.Block), str(bma.blockchain.Parameters)] def __init__(self, community): ''' @@ -49,8 +49,7 @@ class Cache(): :return: The cache as a dict in json format ''' - data = {k: self.data[k] for k in self.data.keys() - if k[0] in Cache._saved_requests} + data = {k: self.data[k] for k in self.data.keys()} entries = [] for d in data: entries.append({'key': d, @@ -63,9 +62,10 @@ class Cache(): Refreshing the cache just clears every last requests which cannot be saved because they can change from one block to another. ''' - self.latest_block = self.community.current_blockid()['number'] - self.data = {k: self.data[k] for k in self.data.keys() - if k[0] in Cache._saved_requests} + if self.latest_block < self.community.network.latest_block: + self.latest_block = self.community.network.latest_block + self.data = {k: self.data[k] for k in self.data.keys() + if k[0] in Cache._saved_requests} def request(self, request, req_args={}, get_args={}): ''' diff --git a/src/cutecoin/core/net/node.py b/src/cutecoin/core/net/node.py index 6f61f393e156e58cf2cde156b7aa3973b1c916aa..6d131a7524c4daa1c1aeb83be97110c102e7db28 100644 --- a/src/cutecoin/core/net/node.py +++ b/src/cutecoin/core/net/node.py @@ -7,11 +7,13 @@ Created on 21 févr. 2015 from ucoinpy.documents.peer import Peer, BMAEndpoint, Endpoint from ucoinpy.api import bma from ucoinpy.api.bma import ConnectionHandler -from requests.exceptions import RequestException +from requests.exceptions import RequestException, ConnectionError from ...tools.exceptions import InvalidNodeCurrency, PersonNotFoundError from ..person import Person import logging import time +import ctypes +import sys from PyQt5.QtCore import QObject, pyqtSignal @@ -214,7 +216,7 @@ class Node(QObject): uid = "" return uid - def refresh_state(self, init=False): + def refresh_state(self): logging.debug("Refresh state") emit_change = False try: @@ -237,23 +239,36 @@ class Node(QObject): node_currency = informations["currency"] node_uid = self._request_uid() + logging.debug("Continuing...") #If the nodes goes back online... if self.state in (Node.OFFLINE, Node.CORRUPTED): self.state = Node.ONLINE logging.debug("Change : new state online") emit_change = True - except RequestException: + except ConnectionError as e: + logging.debug(str(e)) + + if self.state != Node.OFFLINE: + self.state = Node.OFFLINE + logging.debug("Change : new state offine") + emit_change = True + # Dirty hack to reload resolv.conf on linux + if 'Connection aborted' in str(e) and 'gaierror' in str(e): + logging.debug("Connection Aborted") + if 'linux' in sys.platform: + try: + libc = ctypes.CDLL('libc.so.6') + res_init = getattr(libc, '__res_init') + res_init(None) + except: + logging.error('Error calling libc.__res_init') + except RequestException as e: if self.state != Node.OFFLINE: self.state = Node.OFFLINE logging.debug("Change : new state offine") emit_change = True # If not is offline, do not refresh last data - if init: - self.block = block_number - self._pubkey = node_pubkey - self._uid = node_uid - self._neighbours = neighbours if self.state != Node.OFFLINE: # If not changed its currency, consider it corrupted if node_currency != self._currency: diff --git a/src/cutecoin/core/watching/blockchain.py b/src/cutecoin/core/watching/blockchain.py index 4f00b319018687e0e88c54bd525ff58b265619ee..cc6ae2746b688b3ca18b45f279b9bfc4173d697c 100644 --- a/src/cutecoin/core/watching/blockchain.py +++ b/src/cutecoin/core/watching/blockchain.py @@ -18,13 +18,11 @@ class BlockchainWatcher(Watcher): self.community = community self.time_to_wait = int(self.community.parameters['avgGenTime'] / 10) self.exiting = False - blockid = self.community.current_blockid() - self.last_block = blockid['number'] + self.last_block = self.community.network.latest_block def watch(self): try: - blockid = self.community.current_blockid() - block_number = blockid['number'] + block_number = self.community.network.latest_block if self.last_block != block_number: if not self.exiting: self.community.refresh_cache() diff --git a/src/cutecoin/core/watching/network.py b/src/cutecoin/core/watching/network.py deleted file mode 100644 index 0f7e8b094c73e89f9621f559cfbce355c99223ce..0000000000000000000000000000000000000000 --- a/src/cutecoin/core/watching/network.py +++ /dev/null @@ -1,26 +0,0 @@ -''' -Created on 27 févr. 2015 - -@author: inso -''' - -from .watcher import Watcher - - -class NetworkWatcher(Watcher): - ''' - This will crawl the network to always - have up to date informations about the nodes - ''' - - def __init__(self, community): - super().__init__() - self.community = community - - def watch(self): - self.community.network.stopped_perpetual_crawling.connect(self.watching_stopped) - self.community.network.start_perpetual_crawling() - - def stop(self): - self.community.network.stop_crawling() - diff --git a/src/cutecoin/gui/mainwindow.py b/src/cutecoin/gui/mainwindow.py index 27bc567a3abe5f5c8497541362bcd5f94445a9e8..a9604757f95d3bbdd41d4ede46c5e5de5ba9f3fe 100644 --- a/src/cutecoin/gui/mainwindow.py +++ b/src/cutecoin/gui/mainwindow.py @@ -44,15 +44,8 @@ class Loader(QObject): @pyqtSlot() def load(self): if self.account_name != "": - try: - account = self.app.get_account(self.account_name) - self.app.change_current_account(account) - except requests.exceptions.RequestException as e: - self.connection_error.emit(str(e)) - self.loaded.emit() - except NoPeerAvailable as e: - self.connection_error.emit(str(e)) - self.loaded.emit() + account = self.app.get_account(self.account_name) + self.app.change_current_account(account) self.loaded.emit()