diff --git a/src/cutecoin/core/community.py b/src/cutecoin/core/community.py index 8098594c2005db64d6df90a06983eae9c2d4e589..1fcac0a14805dbf7df1716268e2b132af1696ca2 100644 --- a/src/cutecoin/core/community.py +++ b/src/cutecoin/core/community.py @@ -183,7 +183,7 @@ class Community(QObject): :return: The monetary mass value """ # Get cached block by block number - block_number = self.network.latest_block_number + block_number = self.network.current_blockid.number if block_number: block = yield from self.bma_access.future_request(bma.blockchain.Block, req_args={'number': block_number}) @@ -200,7 +200,7 @@ class Community(QObject): """ try: # Get cached block by block number - block_number = self.network.latest_block_number + block_number = self.network.current_blockid.number block = yield from self.bma_access.future_request(bma.blockchain.Block, req_args={'number': block_number}) return block['membersCount'] diff --git a/src/cutecoin/core/graph.py b/src/cutecoin/core/graph.py index aaba1edac431a67231961d23f65c15ac69611f25..9144bf05001740e69b83c452caead6c5d11c59ae 100644 --- a/src/cutecoin/core/graph.py +++ b/src/cutecoin/core/graph.py @@ -221,15 +221,15 @@ class Graph(object): 'cert_time': certifier['cert_time'] } - latest_block_number = self.community.network.latest_block_number - if latest_block_number and certifier['block_number']: - current_validations = latest_block_number - certifier['block_number'] + current_block_number = self.community.network.current_blockid.number + if current_block_number and certifier['block_number']: + current_validations = current_block_number - certifier['block_number'] else: current_validations = 0 members_pubkeys = yield from self.community.members_pubkeys() max_validation = self.community.network.fork_window(members_pubkeys) + 1 - # Current validation can be negative if self.community.network.latest_block_number + # Current validation can be negative if self.community.network.current_blockid.number # is not refreshed yet if max_validation > current_validations > 0: if self.app.preferences['expert_mode']: @@ -299,9 +299,9 @@ class Graph(object): 'cert_time': certified['cert_time'] } - latest_block_number = self.community.network.latest_block_number - if latest_block_number and certified['block_number']: - current_validations = latest_block_number - certified['block_number'] + current_block_number = self.community.network.current_blockid.number + if current_block_number and certified['block_number']: + current_validations = current_block_number - certified['block_number'] else: current_validations = 0 members_pubkeys = yield from self.community.members_pubkeys() diff --git a/src/cutecoin/core/net/api/bma/access.py b/src/cutecoin/core/net/api/bma/access.py index b9e444ddcfee8d079f68851f103e61ccb6b91eac..269a62ee5e65f9a164a764f5562cf57f2382deaa 100644 --- a/src/cutecoin/core/net/api/bma/access.py +++ b/src/cutecoin/core/net/api/bma/access.py @@ -106,7 +106,7 @@ class BmaAccess(QObject): cached_data = self._data[cache_key] need_reload = True if str(request) in BmaAccess.__saved_requests \ - or cached_data['metadata']['block_hash'] == self._network.latest_block_hash: + or cached_data['metadata']['block_hash'] == self._network.current_blockid.sha_hash: need_reload = False ret_data = cached_data['value'] else: @@ -129,8 +129,8 @@ class BmaAccess(QObject): self._data[cache_key] = {'metadata': {}, 'value': {}} - self._data[cache_key]['metadata']['block_number'] = self._network.latest_block_number - self._data[cache_key]['metadata']['block_hash'] = self._network.latest_block_hash + self._data[cache_key]['metadata']['block_number'] = self._network.current_blockid.number + self._data[cache_key]['metadata']['block_hash'] = self._network.current_blockid.sha_hash self._data[cache_key]['metadata']['cutecoin_version'] = __version__ if not self._compare_json(self._data[cache_key]['value'], data): self._data[cache_key]['value'] = data diff --git a/src/cutecoin/core/net/network.py b/src/cutecoin/core/net/network.py index 1d6030ff3f6b31a7180a40ab2cc3bb519de58270..be84b6043c67024179623723a0521a82357fcd85 100644 --- a/src/cutecoin/core/net/network.py +++ b/src/cutecoin/core/net/network.py @@ -10,7 +10,7 @@ import statistics import time import asyncio from ucoinpy.documents.peer import Peer -from ucoinpy.documents.block import Block +from ucoinpy.documents.block import Block, BlockId from PyQt5.QtCore import pyqtSignal, pyqtSlot, QObject, QTimer from collections import Counter @@ -39,7 +39,7 @@ class Network(QObject): self.add_node(n) self.currency = currency self._must_crawl = False - self._refresh_block_found() + self._block_found = self.current_blockid self._timer = QTimer() @classmethod @@ -165,32 +165,16 @@ class Network(QObject): return self._root_nodes @property - def latest_block_number(self): + def current_blockid(self): """ Get the latest block considered valid It is the most frequent last block of every known nodes """ - blocks_numbers = [n.block['number'] for n in self.synced_nodes if n.block] - if len(blocks_numbers) > 0: - return blocks_numbers[0] + blocks = [n.block for n in self.synced_nodes if n.block] + if len(blocks) > 0: + return BlockId(blocks[0]['number'], blocks[0]['hash']) else: - return None - - @property - def latest_block_hash(self): - """ - Get the latest block considered valid - It is the most frequent last block of every known nodes - """ - blocks_hash = [n.block['hash'] for n in self.synced_nodes if n.block] - if len(blocks_hash) > 0: - return blocks_hash[0] - else: - return Block.Empty_Hash - - def _refresh_block_found(self): - self._block_found = {'hash': self.latest_block_hash, - 'number': self.latest_block_number} + return BlockId.empty() def check_nodes_sync(self): """ @@ -338,18 +322,19 @@ class Network(QObject): self.nodes_changed.emit() if node.state == Node.ONLINE: - logging.debug("{0} -> {1}".format(self._block_found['hash'][:10], self.latest_block_hash[:10])) - if self._block_found['hash'] != self.latest_block_hash: - logging.debug("Latest block changed : {0}".format(self.latest_block_number)) + logging.debug("{0} -> {1}".format(self._block_found.sha_hash[:10], self.current_blockid.sha_hash[:10])) + if self._block_found.sha_hash != self.current_blockid.sha_hash: + logging.debug("Latest block changed : {0}".format(self.current_blockid.number)) # If new latest block is lower than the previously found one # or if the previously found block is different locally # than in the main chain, we declare a rollback - if self._block_found['number'] and \ - self.latest_block_number <= self._block_found['number'] \ + if self._block_found.number and \ + self.current_blockid.number <= self._block_found.number \ or node.main_chain_previous_block and \ - node.main_chain_previous_block['hash'] != self._block_found['hash']: - self._refresh_block_found() - self.blockchain_rollback.emit(self.latest_block_number) + node.main_chain_previous_block['hash'] != self._block_found.sha_hash: + + self._block_found = self.current_blockid + self.blockchain_rollback.emit(self.current_blockid.number) else: - self._refresh_block_found() - self.new_block_mined.emit(self.latest_block_number) + self._block_found = self.current_blockid + self.new_block_mined.emit(self.current_blockid.number) diff --git a/src/cutecoin/core/txhistory.py b/src/cutecoin/core/txhistory.py index f3a8896dc56e2477cc5f8426d0324544ecf03c49..a6d358f1ab262f2512defdd9865e6774f0a83c63 100644 --- a/src/cutecoin/core/txhistory.py +++ b/src/cutecoin/core/txhistory.py @@ -321,7 +321,8 @@ class TxHistory(): block = None tries += 1 for transfer in [t for t in self._transfers - if t.state in (TransferState.VALIDATING, TransferState.VALIDATED)]: + if t.state in (TransferState.VALIDATING, TransferState.VALIDATED) and + t.blockid.number == block_number]: return not transfer.run_state_transitions((True, block_doc)) @asyncio.coroutine @@ -349,10 +350,10 @@ class TxHistory(): def refresh(self, community, received_list): # We update the block goal try: - latest_block_number = community.network.latest_block_number - if latest_block_number: + current_block_number = community.network.current_blockid.number + if current_block_number: current_block = yield from community.bma_access.future_request(bma.blockchain.Block, - req_args={'number': latest_block_number}) + req_args={'number': current_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... tx_blocks = [tx.blockid.number for tx in self._transfers diff --git a/src/cutecoin/core/txhistory_indexation.py b/src/cutecoin/core/txhistory_indexation.py index 7e6aaf0276cbb6a8c6b90f3f7859d01db216a0e6..b1abc41b4defe05304683c2dbd27709906e9dbfb 100644 --- a/src/cutecoin/core/txhistory_indexation.py +++ b/src/cutecoin/core/txhistory_indexation.py @@ -167,7 +167,7 @@ class TxHistory(): :param list received_list: List of transactions received """ current_block = yield from community.bma_access.future_request(bma.blockchain.Block, - req_args={'number': community.network.latest_block_number}) + req_args={'number': community.network.current_blockid.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 diff --git a/src/cutecoin/gui/community_tile.py b/src/cutecoin/gui/community_tile.py index 473c453a2d442fd262115a167f11c11f5a1f924d..2c437f52928f6454efcdd6884c2f3aea31de0f1a 100644 --- a/src/cutecoin/gui/community_tile.py +++ b/src/cutecoin/gui/community_tile.py @@ -45,7 +45,7 @@ class CommunityTile(QFrame): def handle_nodes_change(self): if len(self.community.network.online_nodes) > 0: - if self.community.network.latest_block_hash == Block.Empty_Hash: + if self.community.network.current_blockid.sha_hash == Block.Empty_Hash: state = CommunityState.NOT_INIT else: state = CommunityState.READY diff --git a/src/cutecoin/gui/community_view.py b/src/cutecoin/gui/community_view.py index a5c6494ea817ebb28edc1ce4e959019271273479..ce8f129b44419a4b68da74b4751a2e8888afbe88 100644 --- a/src/cutecoin/gui/community_view.py +++ b/src/cutecoin/gui/community_view.py @@ -203,11 +203,11 @@ class CommunityWidget(QWidget, Ui_CommunityWidget): if self.community: text = "" - latest_block_number = self.community.network.latest_block_number - if latest_block_number: - text += self.tr(" Block {0}").format(latest_block_number) + current_block_number = self.community.network.current_blockid.number + if current_block_number: + text += self.tr(" Block {0}").format(current_block_number) try: - block = yield from self.community.get_block(latest_block_number) + block = yield from self.community.get_block(current_block_number) text += " ({0})".format(QLocale.toString( QLocale(), QDateTime.fromTime_t(block['medianTime']), diff --git a/src/cutecoin/models/txhistory.py b/src/cutecoin/models/txhistory.py index 4e434d6158ff492736bd50f1efb49a5802cc51f0..c3dbc4ca62c3856ceedff78d8542338bf893a20d 100644 --- a/src/cutecoin/models/txhistory.py +++ b/src/cutecoin/models/txhistory.py @@ -153,9 +153,9 @@ class TxFilterProxyModel(QSortFilterProxyModel): current_validations = 0 if state_data == TransferState.VALIDATING: - latest_block_number = self.community.network.latest_block_number - if latest_block_number: - current_validations = latest_block_number - block_data + current_blockid.number = self.community.network.current_blockid.number + if current_blockid.number: + current_validations = current_blockid.number - block_data max_validations = self.sourceModel().max_validations() if self.app.preferences['expert_mode']: