diff --git a/src/cutecoin/core/community.py b/src/cutecoin/core/community.py index a089bc9cd098b263a1bc7698505897e8512b9ff6..7f38061cb69bc13c2bed374b303bd1c60d5cc788 100644 --- a/src/cutecoin/core/community.py +++ b/src/cutecoin/core/community.py @@ -123,43 +123,48 @@ class Community(object): self.last_block["number"] = block_number 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: - try: - e = next(e for e in peer.endpoints if type(e) is BMAEndpoint) - # 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), - 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 + e = next(e for e in peer.endpoints if type(e) is BMAEndpoint) + if cached: + try: + # 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), + 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 + except ValueError as e: + if '502' in str(e): + continue else: - self.requests_cache[cache_key] = data - except ValueError as e: - if '502' in str(e): - continue - else: - raise - return self.requests_cache[cache_key] + raise + return self.requests_cache[cache_key] + else: + req = request(e.conn_handler(), **req_args) + data = req.get(**get_args) + return data raise NoPeerAvailable(self.currency) def post(self, request, req_args={}, post_args={}): diff --git a/src/cutecoin/core/person.py b/src/cutecoin/core/person.py index 60b24176947bdb280c3be3dd0243decf0e8d8954..3b3ad2a02c4b6bb73178a9c6805956f89c025d1f 100644 --- a/src/cutecoin/core/person.py +++ b/src/cutecoin/core/person.py @@ -26,11 +26,12 @@ class Person(object): self.pubkey = pubkey @classmethod - def lookup(cls, pubkey, community): + def lookup(cls, pubkey, community, cached=True): ''' 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'] logging.debug(results) timestamp = 0 diff --git a/src/cutecoin/gui/process_cfg_community.py b/src/cutecoin/gui/process_cfg_community.py index 3303172cd8e0d60ca1fe3cc01ac92a575fd1884d..b273a4c6bfac757da0c86f20aa9e5948204fd60b 100644 --- a/src/cutecoin/gui/process_cfg_community.py +++ b/src/cutecoin/gui/process_cfg_community.py @@ -166,7 +166,7 @@ class ProcessConfigureCommunity(QDialog, Ui_CommunityConfigurationDialog): def accept(self): try: - Person.lookup(self.account.pubkey, self.community) + Person.lookup(self.account.pubkey, self.community, cached=False) except PersonNotFoundError as e: reply = QMessageBox.question(self, "Pubkey not found", "The public key of your account wasn't found in the community. :\n \