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

Fixed bug when needing to restart cutecoin to join a new community

parent b51edccb
No related branches found
No related tags found
No related merge requests found
...@@ -123,43 +123,48 @@ class Community(object): ...@@ -123,43 +123,48 @@ class Community(object):
self.last_block["number"] = block_number self.last_block["number"] = block_number
self.requests_cache = {} self.requests_cache = {}
def request(self, request, req_args={}, get_args={}): def request(self, request, req_args={}, get_args={}, cached=True):
for peer in self.peers: for peer in self.peers:
try: 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) if cached:
# We request the current block every five minutes try:
# If a new block is mined we reset the cache # We request the current block every five minutes
# If a new block is mined we reset the cache
self.check_current_block(e)
cache_key = (hash(request), self.check_current_block(e)
hash(tuple(frozenset(sorted(req_args.keys())))), cache_key = (hash(request),
hash(tuple(frozenset(sorted(req_args.items())))), hash(tuple(frozenset(sorted(req_args.keys())))),
hash(tuple(frozenset(sorted(get_args.keys())))), hash(tuple(frozenset(sorted(req_args.items())))),
hash(tuple(frozenset(sorted(get_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: if cache_key not in self.requests_cache.keys():
logging.debug("Connecting to {0}:{1}".format(e.server, if e.server:
e.port)) logging.debug("Connecting to {0}:{1}".format(e.server,
else: e.port))
logging.debug("Connecting to {0}:{1}".format(e.ipv4, else:
e.port)) logging.debug("Connecting to {0}:{1}".format(e.ipv4,
e.port))
req = request(e.conn_handler(), **req_args)
data = req.get(**get_args) req = request(e.conn_handler(), **req_args)
if inspect.isgenerator(data): data = req.get(**get_args)
cached_data = [] if inspect.isgenerator(data):
for d in data: cached_data = []
cached_data.append(d) for d in data:
self.requests_cache[cache_key] = cached_data cached_data.append(d)
self.requests_cache[cache_key] = cached_data
else:
self.requests_cache[cache_key] = data
except ValueError as e:
if '502' in str(e):
continue
else: else:
self.requests_cache[cache_key] = data raise
except ValueError as e: return self.requests_cache[cache_key]
if '502' in str(e): else:
continue req = request(e.conn_handler(), **req_args)
else: data = req.get(**get_args)
raise return data
return self.requests_cache[cache_key]
raise NoPeerAvailable(self.currency) raise NoPeerAvailable(self.currency)
def post(self, request, req_args={}, post_args={}): def post(self, request, req_args={}, post_args={}):
......
...@@ -26,11 +26,12 @@ class Person(object): ...@@ -26,11 +26,12 @@ class Person(object):
self.pubkey = pubkey self.pubkey = pubkey
@classmethod @classmethod
def lookup(cls, pubkey, community): def lookup(cls, pubkey, community, cached=True):
''' '''
Create a person from the pubkey found in a community Create a person from the pubkey found in a community
''' '''
data = community.request(bma.wot.Lookup, req_args={'search': pubkey}) data = community.request(bma.wot.Lookup, req_args={'search': pubkey},
cached=cached)
results = data['results'] results = data['results']
logging.debug(results) logging.debug(results)
timestamp = 0 timestamp = 0
......
...@@ -166,7 +166,7 @@ class ProcessConfigureCommunity(QDialog, Ui_CommunityConfigurationDialog): ...@@ -166,7 +166,7 @@ class ProcessConfigureCommunity(QDialog, Ui_CommunityConfigurationDialog):
def accept(self): def accept(self):
try: try:
Person.lookup(self.account.pubkey, self.community) Person.lookup(self.account.pubkey, self.community, cached=False)
except PersonNotFoundError as e: except PersonNotFoundError as e:
reply = QMessageBox.question(self, "Pubkey not found", reply = QMessageBox.question(self, "Pubkey not found",
"The public key of your account wasn't found in the community. :\n \ "The public key of your account wasn't found in the community. :\n \
......
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