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

Fix crash when nodes are mining for the wrong currency

parent 06d5e365
No related branches found
No related tags found
No related merge requests found
from PyQt5.QtCore import QObject, pyqtSlot from PyQt5.QtCore import QObject, pyqtSlot
from PyQt5.QtNetwork import QNetworkReply from PyQt5.QtNetwork import QNetworkReply
from ucoinpy.api.bma import blockchain from ucoinpy.api import bma
from ucoinpy.documents import Block, BlockId from ucoinpy.documents import Block, BlockId
from .....tools.exceptions import NoPeerAvailable from .....tools.exceptions import NoPeerAvailable
from ..... import __version__ from ..... import __version__
...@@ -15,7 +15,7 @@ class BmaAccess(QObject): ...@@ -15,7 +15,7 @@ class BmaAccess(QObject):
This class is used to access BMA API. This class is used to access BMA API.
""" """
__saved_requests = [str(blockchain.Block), str(blockchain.Parameters)] __saved_requests = [str(bma.blockchain.Block), str(bma.blockchain.Parameters)]
def __init__(self, data, network): def __init__(self, data, network):
""" """
...@@ -112,10 +112,10 @@ class BmaAccess(QObject): ...@@ -112,10 +112,10 @@ class BmaAccess(QObject):
# If we detected a rollback # If we detected a rollback
# We reload if we don't know if this block changed or not # We reload if we don't know if this block changed or not
if self._rollback_to: if self._rollback_to:
if request is blockchain.Block: if request is bma.blockchain.Block:
if get_args["number"] >= self._rollback_to: if get_args["number"] >= self._rollback_to:
need_reload = True need_reload = True
if request is blockchain.Parameters and self._rollback_to == 0: if request is bma.blockchain.Parameters and self._rollback_to == 0:
need_reload = True need_reload = True
elif str(request) in BmaAccess.__saved_requests \ elif str(request) in BmaAccess.__saved_requests \
or cached_data['metadata']['block_hash'] == self._network.current_blockid.sha_hash: or cached_data['metadata']['block_hash'] == self._network.current_blockid.sha_hash:
...@@ -134,7 +134,7 @@ class BmaAccess(QObject): ...@@ -134,7 +134,7 @@ class BmaAccess(QObject):
:param dict get_args: Arguments to pass to the request __get__ method :param dict get_args: Arguments to pass to the request __get__ method
:param dict data: Json data got from the blockchain :param dict data: Json data got from the blockchain
""" """
if self._rollback_to and request is blockchain.Block: if self._rollback_to and request is bma.blockchain.Block:
if get_args['number'] >= self._rollback_to: if get_args['number'] >= self._rollback_to:
cache_key = BmaAccess._gen_cache_key(request, req_args, get_args) cache_key = BmaAccess._gen_cache_key(request, req_args, get_args)
if cache_key in self._data and self._data[cache_key]['value']['hash'] == data['hash']: if cache_key in self._data and self._data[cache_key]['value']['hash'] == data['hash']:
...@@ -171,6 +171,16 @@ class BmaAccess(QObject): ...@@ -171,6 +171,16 @@ class BmaAccess(QObject):
""" """
self._rollback_to = 0 self._rollback_to = 0
def filter_nodes(self, request, nodes):
filters = {
bma.ud.History: lambda n: int(n.version.split(".")[1]) > 11,
bma.tx.History: lambda n: int(n.version.split(".")[1]) > 11
}
if request in filters:
return [n for n in nodes if filters[request](n)]
else:
return nodes
@asyncio.coroutine @asyncio.coroutine
def future_request(self, request, req_args={}, get_args={}): def future_request(self, request, req_args={}, get_args={}):
""" """
...@@ -186,7 +196,7 @@ class BmaAccess(QObject): ...@@ -186,7 +196,7 @@ class BmaAccess(QObject):
need_reload = data[0] need_reload = data[0]
json_data = data[1] json_data = data[1]
nodes = self._network.synced_nodes nodes = self.filter_nodes(request, self._network.synced_nodes)
if need_reload and len(nodes) > 0: if need_reload and len(nodes) > 0:
tries = 0 tries = 0
while tries < 3: while tries < 3:
...@@ -219,7 +229,7 @@ class BmaAccess(QObject): ...@@ -219,7 +229,7 @@ class BmaAccess(QObject):
:param dict get_args: Arguments to pass to the request __get__ method :param dict get_args: Arguments to pass to the request __get__ method
:return: The returned data if cached = True else return the QNetworkReply :return: The returned data if cached = True else return the QNetworkReply
""" """
nodes = self._network.synced_nodes nodes = self.filter_nodes(request, self._network.synced_nodes)
if len(nodes) > 0: if len(nodes) > 0:
node = random.choice(nodes) node = random.choice(nodes)
req = request(node.endpoint.conn_handler(), **req_args) req = request(node.endpoint.conn_handler(), **req_args)
......
...@@ -3,7 +3,8 @@ Created on 24 févr. 2015 ...@@ -3,7 +3,8 @@ Created on 24 févr. 2015
@author: inso @author: inso
""" """
from cutecoin.core.net.node import Node from .node import Node
from ...tools.exceptions import InvalidNodeCurrency
import logging import logging
import statistics import statistics
...@@ -306,9 +307,12 @@ class Network(QObject): ...@@ -306,9 +307,12 @@ class Network(QObject):
pubkeys = [n.pubkey for n in self.nodes] pubkeys = [n.pubkey for n in self.nodes]
if peer.pubkey not in pubkeys: if peer.pubkey not in pubkeys:
logging.debug("New node found : {0}".format(peer.pubkey[:5])) logging.debug("New node found : {0}".format(peer.pubkey[:5]))
node = Node.from_peer(self.currency, peer, pubkey) try:
self.add_node(node) node = Node.from_peer(self.currency, peer, pubkey)
self.nodes_changed.emit() self.add_node(node)
self.nodes_changed.emit()
except InvalidNodeCurrency as e:
logging.debug(str(e))
@pyqtSlot() @pyqtSlot()
def handle_change(self): def handle_change(self):
......
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