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

Refactoring of methods to get network current block

parent 3eaf0a82
No related branches found
No related tags found
No related merge requests found
......@@ -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']
......
......@@ -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()
......
......@@ -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
......
......@@ -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)
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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']),
......
......@@ -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']:
......
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