Skip to content

Commit

Permalink
Use a pool of root servers
Browse files Browse the repository at this point in the history
  • Loading branch information
Insoleet committed Jan 16, 2017
1 parent ad7844e commit f0827bb
Show file tree
Hide file tree
Showing 27 changed files with 246 additions and 312 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Expand Up @@ -6,3 +6,4 @@ duniter-mirage
duniterpy>=0.40
pytest
pytest-asyncio
pyyaml
3 changes: 3 additions & 0 deletions sakia.spec
Expand Up @@ -29,6 +29,7 @@ if is_darwin:
"libsodium.dylib")
a.binaries = a.binaries + TOC([('lib/libsodium.dylib', libsodium_path, 'BINARY')])
a.datas = a.datas + [('sakia/data/repositories/meta.sql', 'src/sakia/data/repositories/meta.sql', 'DATA')]
a.datas = a.datas + [('sakia/root_servers.yml', 'src/sakia/root_servers.yml', 'DATA')]

if is_linux:
libsodium_path = ctypes.util.find_library('libsodium.so')
Expand All @@ -40,10 +41,12 @@ if is_linux:

a.binaries = a.binaries + TOC([('libsodium.so', libsodium_path, 'BINARY')])
a.datas = a.datas + [('sakia/data/repositories/meta.sql', 'src/sakia/data/repositories/meta.sql', 'DATA')]
a.datas = a.datas + [('sakia/root_servers.yml', 'src/sakia/root_servers.yml', 'DATA')]

if is_win:
a.binaries = a.binaries + TOC([('libsodium.dll', ctypes.util.find_library('libsodium.dll'), 'BINARY')])
a.datas = a.datas + [('sakia\\data\\repositories\\meta.sql', 'src\\sakia\\data\\repositories\\meta.sql', 'DATA')]
a.datas = a.datas + [('sakia\\root_servers.yml', 'src\\/sakia\\root_servers.yml', 'DATA')]


print(a.binaries)
Expand Down
80 changes: 35 additions & 45 deletions src/sakia/app.py
Expand Up @@ -33,11 +33,11 @@ class Application(QObject):
:param sakia.data.entities.AppData app_data: the application data
:param sakia.data.entities.UserParameters parameters: the application current user parameters
:param sakia.data.repositories.SakiaDatabase db: The database
:param dict network_services: All network services for current currency
:param dict blockchain_services: All blockchain services for current currency
:param dict identities_services: All identities services for current currency
:param dict sources_services: All sources services for current currency
:param dict transactions_services: All transactions services for current currency
:param sakia.services.NetworkService network_service: All network services for current currency
:param sakia.services.BlockchainService blockchain_service: All blockchain services for current currency
:param sakia.services.IdentitiesService identities_service: All identities services for current currency
:param sakia.services.SourcesService sources_service: All sources services for current currency
:param sakia.Services.TransactionsService transactions_service: All transactions services for current currency
:param sakia.services.DocumentsService documents_service: A service to broadcast documents
"""

Expand All @@ -55,11 +55,12 @@ class Application(QObject):
app_data = attr.ib()
parameters = attr.ib()
db = attr.ib()
network_services = attr.ib(default=attr.Factory(dict))
blockchain_services = attr.ib(default=attr.Factory(dict))
identities_services = attr.ib(default=attr.Factory(dict))
sources_services = attr.ib(default=attr.Factory(dict))
transactions_services = attr.ib(default=attr.Factory(dict))
currency = attr.ib()
network_service = attr.ib(default=attr.Factory(dict))
blockchain_service = attr.ib(default=attr.Factory(dict))
identities_service = attr.ib(default=attr.Factory(dict))
sources_service = attr.ib(default=attr.Factory(dict))
transactions_service = attr.ib(default=attr.Factory(dict))
documents_service = attr.ib(default=None)
current_ref = attr.ib(default=Quantitative)
_logger = attr.ib(default=attr.Factory(lambda:logging.getLogger('sakia')))
Expand All @@ -75,7 +76,7 @@ def __attrs_post_init__(self):
def startup(cls, argv, qapp, loop):
options = SakiaOptions.from_arguments(argv)
app_data = AppDataFile.in_config_path(options.config_path).load_or_init()
app = cls(qapp, loop, options, app_data, None, None)
app = cls(qapp, loop, options, app_data, None, None, options.currency)
#app.set_proxy()
app.get_last_version()
app.load_profile(app_data.default)
Expand Down Expand Up @@ -105,40 +106,31 @@ def instanciate_services(self):
sources_processor = SourcesProcessor.instanciate(self)
transactions_processor = TransactionsProcessor.instanciate(self)
dividends_processor = DividendsProcessor.instanciate(self)
nodes_processor.initialize_root_nodes(self.currency)
self.db.commit()

self.blockchain_services = {}
self.network_services = {}
self.identities_services = {}
self.sources_services = {}
self.transactions_services = {}
self.documents_service = DocumentsService.instanciate(self)

for currency in self.db.connections_repo.get_currencies():
if currency not in self.identities_services:
self.identities_services[currency] = IdentitiesService(currency, connections_processor,
identities_processor,
certs_processor, blockchain_processor,
self.identities_service = IdentitiesService(self.currency, connections_processor,
identities_processor,
certs_processor, blockchain_processor,
bma_connector)

self.transactions_service = TransactionsService(self.currency, transactions_processor,
dividends_processor,
identities_processor, connections_processor,
bma_connector)

if currency not in self.transactions_services:
self.transactions_services[currency] = TransactionsService(currency, transactions_processor,
dividends_processor,
identities_processor, connections_processor,
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:
self.blockchain_services[currency] = BlockchainService(self, currency, blockchain_processor, bma_connector,
self.identities_services[currency],
self.transactions_services[currency],
self.sources_services[currency])
if currency not in self.network_services:
self.network_services[currency] = NetworkService.load(self, currency, nodes_processor,
self.blockchain_services[currency],
self.identities_services[currency])
self.sources_service = SourcesServices(self.currency, sources_processor,
connections_processor, bma_connector)

self.blockchain_service = BlockchainService(self, self.currency, blockchain_processor, bma_connector,
self.identities_service,
self.transactions_service,
self.sources_service)

self.network_service = NetworkService.load(self, self.currency, nodes_processor,
self.blockchain_service,
self.identities_service)

async def remove_connection(self, connection):
await self.stop_current_profile()
Expand Down Expand Up @@ -179,16 +171,14 @@ def switch_language(self):
logging.debug("Couldn't load translation")

def start_coroutines(self):
for currency in self.db.connections_repo.get_currencies():
self.network_services[currency].start_coroutines()
self.network_service.start_coroutines()

async def stop_current_profile(self, closing=False):
"""
Save the account to the cache
and stop the coroutines
"""
for currency in self.db.connections_repo.get_currencies():
await self.network_services[currency].stop_coroutines(closing)
await self.network_service.stop_coroutines(closing)

@asyncify
async def get_last_version(self):
Expand Down
6 changes: 6 additions & 0 deletions src/sakia/constants.py
@@ -1 +1,7 @@
import os
import yaml

MAX_CONFIRMATIONS = 6

with open(os.path.join(os.path.dirname(__file__), "root_servers.yml"), 'r') as stream:
ROOT_SERVERS = yaml.load(stream)
14 changes: 8 additions & 6 deletions src/sakia/data/processors/blockchain.py
Expand Up @@ -26,6 +26,9 @@ def instanciate(cls, app):
return cls(app.db.blockchains_repo,
BmaConnector(NodesProcessor(app.db.nodes_repo), app.parameters))

def initialized(self, currency):
return self._repo.get_one(currency=currency) is not None

async def timestamp(self, currency, block_number):
try:
block = await self._bma_connector.get(currency, bma.blockchain.block, {'number': block_number})
Expand Down Expand Up @@ -139,7 +142,6 @@ async def get_block(self, currency, number):
block_doc = Block.from_signed_raw("{0}{1}\n".format(block['raw'], block['signature']))
return block_doc


async def new_blocks_with_identities(self, currency):
"""
Get blocks more recent than local blockuid
Expand Down Expand Up @@ -209,7 +211,7 @@ async def initialize_blockchain(self, currency, log_stream):
blockchain = Blockchain(currency=currency)
log_stream("Requesting blockchain parameters")
try:
parameters = await self._bma_connector.get(currency, bma.blockchain.parameters, verify=False)
parameters = await self._bma_connector.get(currency, bma.blockchain.parameters)
blockchain.parameters.ms_validity = parameters['msValidity']
blockchain.parameters.avg_gen_time = parameters['avgGenTime']
blockchain.parameters.c = parameters['c']
Expand All @@ -233,7 +235,7 @@ async def initialize_blockchain(self, currency, log_stream):

log_stream("Requesting current block")
try:
current_block = await self._bma_connector.get(currency, bma.blockchain.current, verify=False)
current_block = await self._bma_connector.get(currency, bma.blockchain.current)
signed_raw = "{0}{1}\n".format(current_block['raw'], current_block['signature'])
block = Block.from_signed_raw(signed_raw)
blockchain.current_buid = block.blockUID
Expand All @@ -244,7 +246,7 @@ async def initialize_blockchain(self, currency, log_stream):
raise

log_stream("Requesting blocks with dividend")
with_ud = await self._bma_connector.get(currency, bma.blockchain.ud, verify=False)
with_ud = await self._bma_connector.get(currency, bma.blockchain.ud)
blocks_with_ud = with_ud['result']['blocks']

if len(blocks_with_ud) > 0:
Expand All @@ -253,7 +255,7 @@ async def initialize_blockchain(self, currency, log_stream):
index = max(len(blocks_with_ud) - 1, 0)
block_number = blocks_with_ud[index]
block_with_ud = await self._bma_connector.get(currency, bma.blockchain.block,
req_args={'number': block_number}, verify=False)
req_args={'number': block_number})
if block_with_ud:
blockchain.last_members_count = block_with_ud['membersCount']
blockchain.last_ud = block_with_ud['dividend']
Expand All @@ -269,7 +271,7 @@ async def initialize_blockchain(self, currency, log_stream):
index = max(len(blocks_with_ud) - 2, 0)
block_number = blocks_with_ud[index]
block_with_ud = await self._bma_connector.get(currency, bma.blockchain.block,
req_args={'number': block_number}, verify=False)
req_args={'number': block_number})
blockchain.previous_mass = block_with_ud['monetaryMass']
blockchain.previous_members_count = block_with_ud['membersCount']
blockchain.previous_ud = block_with_ud['dividend']
Expand Down
4 changes: 2 additions & 2 deletions src/sakia/data/processors/certifications.py
Expand Up @@ -103,7 +103,7 @@ async def initialize_certifications(self, identity, log_stream):
certifiers = list()
try:
data = await self._bma_connector.get(identity.currency, bma.wot.certifiers_of,
req_args={'search': identity.pubkey}, verify=False)
req_args={'search': identity.pubkey})

for certifier_data in data['certifications']:
certification = Certification(currency=identity.currency,
Expand Down Expand Up @@ -132,7 +132,7 @@ async def initialize_certifications(self, identity, log_stream):
certified = list()
try:
data = await self._bma_connector.get(identity.currency, bma.wot.certified_by,
req_args={'search': identity.pubkey}, verify=False)
req_args={'search': identity.pubkey})
for certified_data in data['certifications']:
certification = Certification(currency=identity.currency,
certifier=identity.pubkey,
Expand Down
5 changes: 2 additions & 3 deletions src/sakia/data/processors/dividends.py
Expand Up @@ -44,7 +44,7 @@ async def initialize_dividends(self, connection, transactions, log_stream):
:param function log_stream:
"""
history_data = await self._bma_connector.get(connection.currency, bma.ud.history,
req_args={'pubkey': connection.pubkey}, verify=False)
req_args={'pubkey': connection.pubkey})
log_stream("Found {0} available dividends".format(len(history_data["history"]["history"])))
block_numbers = []
for ud_data in history_data["history"]["history"]:
Expand All @@ -66,8 +66,7 @@ async def initialize_dividends(self, connection, transactions, log_stream):
for input in txdoc.inputs:
if input.source == "D" and input.origin_id == connection.pubkey and input.index not in block_numbers:
block = await self._bma_connector.get(connection.currency,
bma.blockchain.block, req_args={'number': input.index},
verify=False)
bma.blockchain.block, req_args={'number': input.index})
await asyncio.sleep(0.5)

dividend = Dividend(currency=connection.currency,
Expand Down

0 comments on commit f0827bb

Please sign in to comment.