diff --git a/src/cutecoin/core/account.py b/src/cutecoin/core/account.py index ac949e699d26c9e17c8b2229765ab9c8c23161c3..3576096a9889f1e6f072f30a78f9f2d9046cb387 100644 --- a/src/cutecoin/core/account.py +++ b/src/cutecoin/core/account.py @@ -58,7 +58,6 @@ class Account(QObject): self.communities = communities self.wallets = wallets self.contacts = contacts - self._refreshing = False self._identities_registry = identities_registry self._current_ref = 0 @@ -155,38 +154,36 @@ class Account(QObject): .. note:: emit the Account pyqtSignal loading_progressed during refresh """ logging.debug("Start refresh transactions") - if not self._refreshing: - self._refreshing = True - loaded_wallets = 0 - received_list = [] - values = {} - maximums = {} - - def progressing(value, maximum, hash): - #logging.debug("Loading = {0} : {1} : {2}".format(value, maximum, loaded_wallets)) - values[hash] = value - maximums[hash] = maximum - account_value = sum(values.values()) - account_max = sum(maximums.values()) - self.loading_progressed.emit(community, account_value, account_max) - - def wallet_finished(received): - logging.debug("Finished loading wallet") - nonlocal loaded_wallets - loaded_wallets += 1 - if loaded_wallets == len(self.wallets): - logging.debug("All wallets loaded") - self._refreshing = False - self.loading_finished.emit(community, received_list) - for w in self.wallets: - w.refresh_progressed.disconnect(progressing) - w.refresh_finished.disconnect(wallet_finished) - - for w in self.wallets: - w.refresh_progressed.connect(progressing) - w.refresh_finished.connect(wallet_finished) - w.init_cache(app, community) - w.refresh_transactions(community, received_list) + loaded_wallets = 0 + received_list = [] + values = {} + maximums = {} + + def progressing(value, maximum, hash): + #logging.debug("Loading = {0} : {1} : {2}".format(value, maximum, loaded_wallets)) + values[hash] = value + maximums[hash] = maximum + account_value = sum(values.values()) + account_max = sum(maximums.values()) + self.loading_progressed.emit(community, account_value, account_max) + + def wallet_finished(received): + logging.debug("Finished loading wallet") + nonlocal loaded_wallets + loaded_wallets += 1 + if loaded_wallets == len(self.wallets): + logging.debug("All wallets loaded") + self._refreshing = False + self.loading_finished.emit(community, received_list) + for w in self.wallets: + w.refresh_progressed.disconnect(progressing) + w.refresh_finished.disconnect(wallet_finished) + + for w in self.wallets: + w.refresh_progressed.connect(progressing) + w.refresh_finished.connect(wallet_finished) + w.init_cache(app, community) + w.refresh_transactions(community, received_list) def set_display_referential(self, index): self._current_ref = index diff --git a/src/cutecoin/core/app.py b/src/cutecoin/core/app.py index f63223d057259ecead6f0f861c12cb1a47862198..4ae3e38a99bba709e9d0104f09ab7a5a4546856a 100644 --- a/src/cutecoin/core/app.py +++ b/src/cutecoin/core/app.py @@ -171,11 +171,6 @@ class Application(QObject): self.current_account = account if self.current_account is not None: - for community in self.current_account.communities: - community.network.new_block_mined.connect(lambda: - self.current_account.refresh_transactions(self, community) - ) - self.current_account.refresh_transactions(self, community) self.current_account.start_coroutines() def stop_current_account(self): @@ -233,6 +228,12 @@ class Application(QObject): self.load_cache(account) self.accounts[account_name] = account + for community in account.communities: + def refresh_tx(blocknumber, co=community): + account.refresh_transactions(self, co) + community.network.new_block_mined.connect(refresh_tx) + account.refresh_transactions(self, community) + def load_cache(self, account): """ Load an account cache diff --git a/src/cutecoin/core/net/network.py b/src/cutecoin/core/net/network.py index c00c8fac572b1dce287bcd61c198a5a53442c0d2..18b50f31ec08c1e16f9dc13e2b55443412be459b 100644 --- a/src/cutecoin/core/net/network.py +++ b/src/cutecoin/core/net/network.py @@ -310,8 +310,8 @@ class Network(QObject): while self.continue_crawling(): for node in self.nodes: if self.continue_crawling(): - yield from asyncio.sleep(15) node.refresh() + yield from asyncio.sleep(15) logging.debug("End of network discovery") @pyqtSlot(Peer, str) diff --git a/src/cutecoin/core/net/node.py b/src/cutecoin/core/net/node.py index 5e72af21cc6658f101fe20b54ec22b3667a95747..ca363b781b8a810da203cc6722d4cf45407cd4b7 100644 --- a/src/cutecoin/core/net/node.py +++ b/src/cutecoin/core/net/node.py @@ -120,7 +120,7 @@ class Node(QObject): fork_window = 0 block = None last_change = time.time() - state = Node.ONLINE + state = Node.OFFLINE logging.debug(data) for endpoint_data in data['endpoints']: endpoints.append(Endpoint.from_inline(endpoint_data)) diff --git a/src/cutecoin/core/txhistory.py b/src/cutecoin/core/txhistory.py index cb1cbfbc59ad36a8b7082edb17a7bee128ba1122..2561ef0fa44a580cf1dea9f19dc612af9b04cec4 100644 --- a/src/cutecoin/core/txhistory.py +++ b/src/cutecoin/core/txhistory.py @@ -304,25 +304,30 @@ class TxHistory(): @asyncio.coroutine def refresh(self, community, received_list): # We update the block goal - current_block = yield from community.bma_access.future_request(bma.blockchain.Block, - req_args={'number': community.network.latest_block_number}) - members_pubkeys = yield from community.members_pubkeys() - # We look for the first block to parse, depending on awaiting and validating transfers and ud... - blocks = [tx.metadata['block'] for tx in self._transfers - if tx.state in (Transfer.AWAITING, Transfer.VALIDATING)] +\ - [ud['block_number'] for ud in self._dividends - if ud['state'] in (Transfer.AWAITING, Transfer.VALIDATING)] +\ - [max(0, self.latest_block - community.network.fork_window(members_pubkeys))] - parsed_block = min(set(blocks)) - self._block_to = current_block - - # We wait for current refresh coroutines - if len(self._running_refresh) > 0: - logging.debug("Wait for the end of previous refresh") - done, pending = yield from asyncio.wait(self._running_refresh) - for cor in done: - self._running_refresh.remove(cor) - - # Then we start a new one - task = asyncio.async(self._refresh(community, parsed_block, received_list)) - self._running_refresh.append(task) \ No newline at end of file + try: + current_block = yield from community.bma_access.future_request(bma.blockchain.Block, + req_args={'number': community.network.latest_block_number}) + members_pubkeys = yield from community.members_pubkeys() + # We look for the first block to parse, depending on awaiting and validating transfers and ud... + blocks = [tx.metadata['block'] for tx in self._transfers + if tx.state in (Transfer.AWAITING, Transfer.VALIDATING)] +\ + [ud['block_number'] for ud in self._dividends + if ud['state'] in (Transfer.AWAITING, Transfer.VALIDATING)] +\ + [max(0, self.latest_block - community.network.fork_window(members_pubkeys))] + parsed_block = min(set(blocks)) + self._block_to = current_block + + # We wait for current refresh coroutines + if len(self._running_refresh) > 0: + logging.debug("Wait for the end of previous refresh") + done, pending = yield from asyncio.wait(self._running_refresh) + for cor in done: + self._running_refresh.remove(cor) + + # Then we start a new one + task = asyncio.async(self._refresh(community, parsed_block, received_list)) + self._running_refresh.append(task) + except ValueError as e: + logging.debug("Block not found") + except NoPeerAvailable: + logging.debug("No peer available") \ No newline at end of file