Skip to content
Snippets Groups Projects
Commit 1d4e670d authored by inso's avatar inso
Browse files

Fixing caching bug

Fixing caching bug with blocks being refreshed only every 5 minutes, even if the blocks changed.
parent 7bec2fae
No related branches found
No related tags found
No related merge requests found
...@@ -112,11 +112,12 @@ class Community(object): ...@@ -112,11 +112,12 @@ class Community(object):
block = bma.blockchain.Current(e.conn_handler()).get() block = bma.blockchain.Current(e.conn_handler()).get()
self.last_block = {"request_ts": time.time(), self.last_block = {"request_ts": time.time(),
"number": block['number']} "number": block['number']}
elif self.last_block["request_ts"] < time.time() - 300: elif self.last_block["request_ts"] + 60 < time.time():
logging.debug("{0} > {1}".format(self.last_block["request_ts"] + 60, time.time()))
self.last_block["request_ts"] = time.time()
block = bma.blockchain.Current(e.conn_handler()).get() block = bma.blockchain.Current(e.conn_handler()).get()
if block['number'] > self.last_block['number']: if block['number'] > self.last_block['number']:
self.last_block = {"request_ts": time.time(), self.last_block["number"] = block['number']
"number": block['number']}
self.requests_cache = {} self.requests_cache = {}
cache_key = (hash(request), cache_key = (hash(request),
......
...@@ -59,23 +59,22 @@ class Cache(): ...@@ -59,23 +59,22 @@ class Cache():
'awaiting': data_awaiting} 'awaiting': data_awaiting}
def latest_sent(self, community): def latest_sent(self, community):
self._refresh(community)
return self.tx_sent return self.tx_sent
def awaiting(self, community): def awaiting(self, community):
self._refresh(community)
return self.awaiting_tx return self.awaiting_tx
def latest_received(self, community): def latest_received(self, community):
self._refresh(community)
return self.tx_received return self.tx_received
def _refresh(self, community): def refresh(self, community):
current_block = community.request(bma.blockchain.Current) current_block = community.request(bma.blockchain.Current)
with_tx = community.request(bma.blockchain.TX) with_tx = community.request(bma.blockchain.TX)
# We parse only blocks with transactions # We parse only blocks with transactions
parsed_blocks = reversed(range(self.latest_block + 1, parsed_blocks = reversed(range(self.latest_block + 1,
current_block['number'] + 1)) current_block['number'] + 1))
logging.debug("Refresh from {0} to {1}".format(self.latest_block + 1,
current_block['number'] + 1))
parsed_blocks = [n for n in parsed_blocks parsed_blocks = [n for n in parsed_blocks
if n in with_tx['result']['blocks']] if n in with_tx['result']['blocks']]
......
...@@ -40,6 +40,8 @@ class CertificationDialog(QDialog, Ui_CertificationDialog): ...@@ -40,6 +40,8 @@ class CertificationDialog(QDialog, Ui_CertificationDialog):
pubkey = self.edit_pubkey.text() pubkey = self.edit_pubkey.text()
password = self.password_asker.ask() password = self.password_asker.ask()
if password == "":
return
try: try:
self.certifier.certify(password, self.community, pubkey) self.certifier.certify(password, self.community, pubkey)
......
...@@ -20,8 +20,9 @@ from ..models.wallet import WalletListModel ...@@ -20,8 +20,9 @@ from ..models.wallet import WalletListModel
class BlockchainInspector(QThread): class BlockchainInspector(QThread):
def __init__(self, community): def __init__(self, account, community):
QThread.__init__(self) QThread.__init__(self)
self.account = account
self.community = community self.community = community
self.exiting = False self.exiting = False
self.last_block = self.community.request(bma.blockchain.Current)['number'] self.last_block = self.community.request(bma.blockchain.Current)['number']
...@@ -31,10 +32,16 @@ class BlockchainInspector(QThread): ...@@ -31,10 +32,16 @@ class BlockchainInspector(QThread):
self.wait() self.wait()
def run(self): def run(self):
logging.debug("Runs.")
while not self.exiting: while not self.exiting:
logging.debug("Sleep.")
time.sleep(10) time.sleep(10)
current_block = self.community.request(bma.blockchain.Current) current_block = self.community.request(bma.blockchain.Current)
logging.debug("Current block... {0}".format(current_block['number']))
if self.last_block != current_block['number']: if self.last_block != current_block['number']:
for w in self.account.wallets:
w.cache.refresh(self.community)
logging.debug("New block, {0} mined in {1}".format(self.last_block, logging.debug("New block, {0} mined in {1}".format(self.last_block,
self.community.currency)) self.community.currency))
self.new_block_mined.emit(current_block['number']) self.new_block_mined.emit(current_block['number'])
...@@ -61,9 +68,10 @@ class CurrencyTabWidget(QWidget, Ui_CurrencyTabWidget): ...@@ -61,9 +68,10 @@ class CurrencyTabWidget(QWidget, Ui_CurrencyTabWidget):
self.tab_community = CommunityTabWidget(self.app.current_account, self.tab_community = CommunityTabWidget(self.app.current_account,
self.community, self.community,
self.password_asker) self.password_asker)
bc_inspector = BlockchainInspector(community) self.bc_inspector = BlockchainInspector(self.app.current_account,
bc_inspector.new_block_mined.connect(self.refresh_block) community)
bc_inspector.start() self.bc_inspector.new_block_mined.connect(self.refresh_block)
self.bc_inspector.start()
def refresh(self): def refresh(self):
if self.app.current_account is None: if self.app.current_account is None:
...@@ -77,12 +85,14 @@ class CurrencyTabWidget(QWidget, Ui_CurrencyTabWidget): ...@@ -77,12 +85,14 @@ class CurrencyTabWidget(QWidget, Ui_CurrencyTabWidget):
self.list_transactions_received.setModel( self.list_transactions_received.setModel(
ReceivedListModel(self.app.current_account, self.community)) ReceivedListModel(self.app.current_account, self.community))
self.tab_community = CommunityTabWidget(self.app.current_account, self.tab_community = CommunityTabWidget(self.app.current_account,
self.community, self.password_asker) self.community,
self.password_asker)
self.tabs_account.addTab(self.tab_community, self.tabs_account.addTab(self.tab_community,
QIcon(':/icons/community_icon'), QIcon(':/icons/community_icon'),
"Community") "Community")
block_number = self.community.request(bma.blockchain.Current)['number'] block_number = self.community.request(bma.blockchain.Current)['number']
self.label_current_block.setText("Current Block : {0}".format(block_number)) self.label_current_block.setText("Current Block : {0}"
.format(block_number))
@pyqtSlot(int) @pyqtSlot(int)
def refresh_block(self, block_number): def refresh_block(self, block_number):
...@@ -108,7 +118,9 @@ class CurrencyTabWidget(QWidget, Ui_CurrencyTabWidget): ...@@ -108,7 +118,9 @@ class CurrencyTabWidget(QWidget, Ui_CurrencyTabWidget):
QModelIndex(), QModelIndex(),
QModelIndex(), QModelIndex(),
[]) [])
self.label_current_block.setText("Current Block : {0}".format(block_number))
self.label_current_block.setText("Current Block : {0}"
.format(block_number))
def refresh_wallets(self): def refresh_wallets(self):
wallets_list_model = WalletsListModel(self.app.current_account, wallets_list_model = WalletsListModel(self.app.current_account,
......
...@@ -100,7 +100,8 @@ class MainWindow(QMainWindow, Ui_MainWindow): ...@@ -100,7 +100,8 @@ class MainWindow(QMainWindow, Ui_MainWindow):
self.action_configure_parameters.setEnabled(False) self.action_configure_parameters.setEnabled(False)
self.action_set_as_default.setEnabled(False) self.action_set_as_default.setEnabled(False)
else: else:
self.action_set_as_default.setEnabled(self.app.current_account.name != self.app.default_account) self.action_set_as_default.setEnabled(self.app.current_account.name
!= self.app.default_account)
self.password_asker = PasswordAskerDialog(self.app.current_account) self.password_asker = PasswordAskerDialog(self.app.current_account)
self.menu_contacts.setEnabled(True) self.menu_contacts.setEnabled(True)
self.action_configure_parameters.setEnabled(True) self.action_configure_parameters.setEnabled(True)
......
...@@ -51,6 +51,8 @@ class TransferMoneyDialog(QDialog, Ui_TransferMoneyDialog): ...@@ -51,6 +51,8 @@ class TransferMoneyDialog(QDialog, Ui_TransferMoneyDialog):
amount = self.spinbox_amount.value() amount = self.spinbox_amount.value()
password = self.password_asker.ask() password = self.password_asker.ask()
if password == "":
return
try: try:
self.wallet.send_money(self.sender.salt, password, self.community, self.wallet.send_money(self.sender.salt, password, self.community,
......
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