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

Refresh sources on new block

parent 761f4f70
No related branches found
No related tags found
No related merge requests found
...@@ -47,6 +47,7 @@ class Application(QObject): ...@@ -47,6 +47,7 @@ class Application(QObject):
identity_changed = pyqtSignal(Identity) identity_changed = pyqtSignal(Identity)
new_connection = pyqtSignal(Connection) new_connection = pyqtSignal(Connection)
referential_changed = pyqtSignal() referential_changed = pyqtSignal()
sources_refreshed = pyqtSignal()
qapp = attr.ib() qapp = attr.ib()
loop = attr.ib() loop = attr.ib()
...@@ -125,16 +126,18 @@ class Application(QObject): ...@@ -125,16 +126,18 @@ class Application(QObject):
identities_processor, connections_processor, identities_processor, connections_processor,
bma_connector) bma_connector)
if currency not in self.sources_services:
self.sources_services[currency] = SourcesServices(currency, sources_processor,
connections_processor, bma_connector)
if currency not in self.blockchain_services: if currency not in self.blockchain_services:
self.blockchain_services[currency] = BlockchainService(self, currency, blockchain_processor, bma_connector, self.blockchain_services[currency] = BlockchainService(self, currency, blockchain_processor, bma_connector,
self.identities_services[currency], self.identities_services[currency],
self.transactions_services[currency]) self.transactions_services[currency],
self.sources_services[currency])
if currency not in self.network_services: if currency not in self.network_services:
self.network_services[currency] = NetworkService.load(self, currency, nodes_processor, self.network_services[currency] = NetworkService.load(self, currency, nodes_processor,
self.blockchain_services[currency]) self.blockchain_services[currency])
if currency not in self.sources_services:
self.sources_services[currency] = SourcesServices(currency, sources_processor, bma_connector)
def switch_language(self): def switch_language(self):
logging.debug("Loading translations") logging.debug("Loading translations")
......
import attr import attr
import re import sqlite3
from ..entities import Source from ..entities import Source
from .nodes import NodesProcessor from .nodes import NodesProcessor
from ..connectors import BmaConnector from ..connectors import BmaConnector
from duniterpy.api import bma, errors from duniterpy.api import bma, errors
import asyncio
@attr.s @attr.s
...@@ -25,12 +24,16 @@ class SourcesProcessor: ...@@ -25,12 +24,16 @@ class SourcesProcessor:
return cls(app.db.sources_repo, return cls(app.db.sources_repo,
BmaConnector(NodesProcessor(app.db.nodes_repo), app.parameters)) BmaConnector(NodesProcessor(app.db.nodes_repo), app.parameters))
def commit(self, source):
try:
self._repo.insert(source)
except sqlite3.IntegrityError:
self._repo.update(source)
async def initialize_sources(self, currency, pubkey, log_stream): async def initialize_sources(self, currency, pubkey, log_stream):
""" """
Initialize sources for a given pubkey if no source exists locally Initialize sources for a given pubkey if no source exists locally
""" """
one_source = self._repo.get_one(currency=currency)
if not one_source:
log_stream("Requesting sources") log_stream("Requesting sources")
try: try:
sources_data = await self._bma_connector.get(currency, bma.tx.sources, sources_data = await self._bma_connector.get(currency, bma.tx.sources,
...@@ -44,8 +47,7 @@ class SourcesProcessor: ...@@ -44,8 +47,7 @@ class SourcesProcessor:
noffset=s['noffset'], noffset=s['noffset'],
amount=s['amount'], amount=s['amount'],
base=s['base']) base=s['base'])
self._repo.insert(source) self.commit(source)
await asyncio.sleep(0)
log_stream("{0}/{1} sources".format(i, len(sources_data['sources']))) log_stream("{0}/{1} sources".format(i, len(sources_data['sources'])))
except errors.DuniterError as e: except errors.DuniterError as e:
raise raise
...@@ -76,3 +78,6 @@ class SourcesProcessor: ...@@ -76,3 +78,6 @@ class SourcesProcessor:
""" """
for s in sources: for s in sources:
self._repo.drop(s) self._repo.drop(s)
def drop_all_of(self, currency, pubkey):
self._repo.drop_all(currency=currency, pubkey=pubkey)
...@@ -69,3 +69,13 @@ class SourcesRepo: ...@@ -69,3 +69,13 @@ class SourcesRepo:
WHERE WHERE
identifier=?""", where_fields) identifier=?""", where_fields)
def drop_all(self, **filter):
filters = []
values = []
for k, v in filter.items():
value = v
filters.append("{key} = ?".format(key=k))
values.append(value)
request = "DELETE FROM sources WHERE {filters}".format(filters=" AND ".join(filters))
self._conn.execute(request, tuple(values))
\ No newline at end of file
...@@ -52,6 +52,8 @@ class InformationsController(QObject): ...@@ -52,6 +52,8 @@ class InformationsController(QObject):
app.identity_changed.connect(informations.handle_identity_change) app.identity_changed.connect(informations.handle_identity_change)
app.new_transfer.connect(informations.refresh_localized_data) app.new_transfer.connect(informations.refresh_localized_data)
app.new_dividend.connect(informations.refresh_localized_data) app.new_dividend.connect(informations.refresh_localized_data)
app.referential_changed.connect(informations.refresh_localized_data)
app.sources_refreshed.connect(informations.refresh_localized_data)
return informations return informations
@asyncify @asyncify
......
...@@ -41,6 +41,7 @@ class TxHistoryController(QObject): ...@@ -41,6 +41,7 @@ class TxHistoryController(QObject):
txhistory = cls(view, model) txhistory = cls(view, model)
model.setParent(txhistory) model.setParent(txhistory)
app.referential_changed.connect(txhistory.refresh_balance) app.referential_changed.connect(txhistory.refresh_balance)
app.sources_refreshed.connect(txhistory.refresh_balance)
return txhistory return txhistory
def refresh_minimum_maximum(self): def refresh_minimum_maximum(self):
......
...@@ -10,7 +10,8 @@ class BlockchainService(QObject): ...@@ -10,7 +10,8 @@ class BlockchainService(QObject):
Blockchain service is managing new blocks received Blockchain service is managing new blocks received
to update data locally to update data locally
""" """
def __init__(self, app, currency, blockchain_processor, bma_connector, identities_service, transactions_service): def __init__(self, app, currency, blockchain_processor, bma_connector,
identities_service, transactions_service, sources_service):
""" """
Constructor the identities service Constructor the identities service
...@@ -20,6 +21,7 @@ class BlockchainService(QObject): ...@@ -20,6 +21,7 @@ class BlockchainService(QObject):
:param sakia.data.connectors.BmaConnector bma_connector: The connector to BMA API :param sakia.data.connectors.BmaConnector bma_connector: The connector to BMA API
:param sakia.services.IdentitiesService identities_service: The identities service :param sakia.services.IdentitiesService identities_service: The identities service
:param sakia.services.TransactionsService transactions_service: The transactions service :param sakia.services.TransactionsService transactions_service: The transactions service
:param sakia.services.SourcesService sources_service: The sources service
""" """
super().__init__() super().__init__()
self.app = app self.app = app
...@@ -28,6 +30,7 @@ class BlockchainService(QObject): ...@@ -28,6 +30,7 @@ class BlockchainService(QObject):
self.currency = currency self.currency = currency
self._identities_service = identities_service self._identities_service = identities_service
self._transactions_service = transactions_service self._transactions_service = transactions_service
self._sources_service = sources_service
self._logger = logging.getLogger('sakia') self._logger = logging.getLogger('sakia')
async def handle_blockchain_progress(self, network_blockstamp): async def handle_blockchain_progress(self, network_blockstamp):
...@@ -44,6 +47,7 @@ class BlockchainService(QObject): ...@@ -44,6 +47,7 @@ class BlockchainService(QObject):
if len(blocks) > 0: if len(blocks) > 0:
identities = await self._identities_service.handle_new_blocks(blocks) identities = await self._identities_service.handle_new_blocks(blocks)
changed_tx, new_tx, new_dividends = await self._transactions_service.handle_new_blocks(blocks) changed_tx, new_tx, new_dividends = await self._transactions_service.handle_new_blocks(blocks)
await self._sources_service.refresh_sources()
self._blockchain_processor.handle_new_blocks(self.currency, blocks) self._blockchain_processor.handle_new_blocks(self.currency, blocks)
self.app.db.commit() self.app.db.commit()
for tx in changed_tx: for tx in changed_tx:
...@@ -54,6 +58,7 @@ class BlockchainService(QObject): ...@@ -54,6 +58,7 @@ class BlockchainService(QObject):
self.app.new_dividend.emit(ud) self.app.new_dividend.emit(ud)
for idty in identities: for idty in identities:
self.app.identity_changed.emit(idty) self.app.identity_changed.emit(idty)
self.app.sources_refreshed.emit()
except (NoPeerAvailable, DuniterError) as e: except (NoPeerAvailable, DuniterError) as e:
self._logger.debug(str(e)) self._logger.debug(str(e))
......
from PyQt5.QtCore import QObject from PyQt5.QtCore import QObject
from duniterpy.api import bma from duniterpy.api import bma, errors
import math
import logging import logging
from sakia.data.entities import Source
class SourcesServices(QObject): class SourcesServices(QObject):
...@@ -9,19 +9,39 @@ class SourcesServices(QObject): ...@@ -9,19 +9,39 @@ class SourcesServices(QObject):
Source service is managing sources received Source service is managing sources received
to update data locally to update data locally
""" """
def __init__(self, currency, sources_processor, bma_connector): def __init__(self, currency, sources_processor, connections_processor, bma_connector):
""" """
Constructor the identities service Constructor the identities service
:param str currency: The currency name of the community :param str currency: The currency name of the community
:param sakia.data.processors.SourceProcessor sources_processor: the sources processor for given currency :param sakia.data.processors.SourcesProcessor sources_processor: the sources processor for given currency
:param sakia.data.processors.ConnectionsProcessor connections_processor: the connections processor
:param sakia.data.connectors.BmaConnector bma_connector: The connector to BMA API :param sakia.data.connectors.BmaConnector bma_connector: The connector to BMA API
""" """
super().__init__() super().__init__()
self._sources_processor = sources_processor self._sources_processor = sources_processor
self._connections_processor = connections_processor
self._bma_connector = bma_connector self._bma_connector = bma_connector
self.currency = currency self.currency = currency
self._logger = logging.getLogger('sakia') self._logger = logging.getLogger('sakia')
def amount(self, pubkey): def amount(self, pubkey):
return self._sources_processor.amount(self.currency, pubkey) return self._sources_processor.amount(self.currency, pubkey)
async def refresh_sources(self):
connections_pubkeys = [c.pubkey for c in self._connections_processor.connections_to(self.currency)]
for pubkey in connections_pubkeys:
sources_data = await self._bma_connector.get(self.currency, bma.tx.sources,
req_args={'pubkey': pubkey})
self._logger.debug("Found {0} sources".format(len(sources_data['sources'])))
self._sources_processor.drop_all_of(currency=self.currency, pubkey=pubkey)
for i, s in enumerate(sources_data['sources']):
source = Source(currency=self.currency, pubkey=pubkey,
identifier=s['identifier'],
type=s['type'],
noffset=s['noffset'],
amount=s['amount'],
base=s['base'])
self._sources_processor.commit(source)
self._logger.debug("{0}/{1} sources".format(i, len(sources_data['sources'])))
from PyQt5.QtCore import QObject from PyQt5.QtCore import QObject
from sakia.data.entities.transaction import parse_transaction_doc from sakia.data.entities.transaction import parse_transaction_doc
from duniterpy.documents import Transaction as TransactionDoc from duniterpy.documents import Transaction as TransactionDoc
from duniterpy.documents import SimpleTransaction
from sakia.data.entities import Dividend from sakia.data.entities import Dividend
from duniterpy.api import bma from duniterpy.api import bma
import logging import logging
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment