2016-09-27 20:18:35 +02:00
|
|
|
from lbrynet.lbrynet_console import Plugin
|
2015-09-04 22:22:02 +02:00
|
|
|
from twisted.internet import defer
|
2016-10-27 21:18:25 +02:00
|
|
|
from lbrynet.conf import settings
|
2015-08-20 17:27:15 +02:00
|
|
|
from BlindRepeater import BlindRepeater
|
|
|
|
from BlindInfoManager import BlindInfoManager
|
|
|
|
from BlindRepeaterSettings import BlindRepeaterSettings
|
|
|
|
from BlindRepeaterControlHandlers import StartRepeaterFactory, StopRepeaterFactory, UpdateMaxSpaceFactory
|
|
|
|
from BlindRepeaterControlHandlers import AddApprovedPeerFactory, DeleteApprovedPeerFactory, RepeaterStatusFactory
|
|
|
|
from BlindRepeaterControlHandlers import ShowApprovedPeersFactory, ModifyRepeaterOptionsFactory
|
|
|
|
from ValuableBlobQueryHandler import ValuableBlobLengthQueryHandlerFactory
|
|
|
|
from ValuableBlobQueryHandler import ValuableBlobHashQueryHandlerFactory
|
|
|
|
|
|
|
|
from PaymentRateManager import BlindRepeaterPaymentRateManager
|
|
|
|
|
|
|
|
|
2016-09-27 20:18:35 +02:00
|
|
|
class BlindRepeaterPlugin(Plugin.Plugin):
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
def __init__(self):
|
2016-09-27 20:18:35 +02:00
|
|
|
Plugin.Plugin.__init__(self)
|
2015-09-17 17:03:57 +02:00
|
|
|
self.enabled = False
|
2015-08-20 17:27:15 +02:00
|
|
|
self.blind_info_manager = None
|
|
|
|
self.valuable_blob_length_query_handler = None
|
|
|
|
self.valuable_blob_hash_query_handler = None
|
|
|
|
self.repeater = None
|
|
|
|
self.control_handlers = None
|
|
|
|
self.payment_rate_manager = None
|
|
|
|
self.settings = None
|
|
|
|
|
|
|
|
def setup(self, lbry_console):
|
2015-09-17 17:03:57 +02:00
|
|
|
if not self.enabled:
|
|
|
|
return defer.succeed(True)
|
2015-08-20 17:27:15 +02:00
|
|
|
lbry_session = lbry_console.session
|
2015-09-04 22:22:02 +02:00
|
|
|
d = self._setup_settings(lbry_session.db_dir)
|
2015-08-20 17:27:15 +02:00
|
|
|
d.addCallback(lambda _: self._get_payment_rate_manager(lbry_session.base_payment_rate_manager))
|
2015-09-04 22:22:02 +02:00
|
|
|
d.addCallback(lambda _: self._setup_blind_info_manager(lbry_session.peer_manager, lbry_session.db_dir))
|
2015-08-20 17:27:15 +02:00
|
|
|
d.addCallback(lambda _: self._setup_blind_repeater(lbry_session))
|
|
|
|
d.addCallback(lambda _: self._setup_valuable_blob_query_handler(lbry_session))
|
|
|
|
d.addCallback(lambda _: self._create_control_handlers(lbry_session))
|
|
|
|
d.addCallback(lambda _: self._restore_repeater_status(lbry_session))
|
|
|
|
d.addCallback(lambda _: self._add_to_lbry_console(lbry_console))
|
|
|
|
return d
|
|
|
|
|
2015-09-04 22:22:02 +02:00
|
|
|
def stop(self):
|
2015-09-17 17:03:57 +02:00
|
|
|
if self.settings is not None:
|
|
|
|
return self.settings.stop()
|
|
|
|
return defer.succeed(True)
|
2015-08-20 17:27:15 +02:00
|
|
|
|
2015-09-04 22:22:02 +02:00
|
|
|
def _setup_settings(self, db_dir):
|
|
|
|
self.settings = BlindRepeaterSettings(db_dir)
|
|
|
|
return self.settings.setup()
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
def _get_payment_rate_manager(self, default_payment_rate_manager):
|
|
|
|
d1 = self.settings.get_data_payment_rate()
|
|
|
|
d2 = self.settings.get_valuable_info_payment_rate()
|
|
|
|
d3 = self.settings.get_valuable_hash_payment_rate()
|
|
|
|
|
|
|
|
dl = defer.DeferredList([d1, d2, d3])
|
|
|
|
|
|
|
|
def get_payment_rate_manager(rates):
|
|
|
|
data_rate = rates[0][1] if rates[0][0] is True else None
|
|
|
|
info_rate = rates[1][1] if rates[1][0] is True else None
|
2016-10-26 09:16:33 +02:00
|
|
|
info_rate = info_rate if info_rate is not None else settings.min_valuable_info_rate
|
2015-08-20 17:27:15 +02:00
|
|
|
hash_rate = rates[2][1] if rates[2][0] is True else None
|
2016-10-26 09:16:33 +02:00
|
|
|
hash_rate = hash_rate if hash_rate is not None else settings.min_valuable_hash_rate
|
2015-08-20 17:27:15 +02:00
|
|
|
self.payment_rate_manager = BlindRepeaterPaymentRateManager(default_payment_rate_manager,
|
|
|
|
info_rate, hash_rate,
|
|
|
|
blob_data_rate=data_rate)
|
|
|
|
|
|
|
|
dl.addCallback(get_payment_rate_manager)
|
|
|
|
return dl
|
|
|
|
|
2015-09-04 22:22:02 +02:00
|
|
|
def _setup_blind_info_manager(self, peer_manager, db_dir):
|
|
|
|
self.blind_info_manager = BlindInfoManager(db_dir, peer_manager)
|
2015-08-20 17:27:15 +02:00
|
|
|
return self.blind_info_manager.setup()
|
|
|
|
|
|
|
|
def _setup_valuable_blob_query_handler(self, lbry_session):
|
|
|
|
self.valuable_blob_length_query_handler = ValuableBlobLengthQueryHandlerFactory(lbry_session.blob_manager,
|
|
|
|
lbry_session.wallet,
|
|
|
|
self.payment_rate_manager)
|
|
|
|
self.valuable_blob_hash_query_handler = ValuableBlobHashQueryHandlerFactory(lbry_session.peer_finder,
|
|
|
|
lbry_session.wallet,
|
|
|
|
self.payment_rate_manager)
|
|
|
|
|
|
|
|
def _setup_blind_repeater(self, lbry_session):
|
|
|
|
self.repeater = BlindRepeater(lbry_session.peer_finder, lbry_session.rate_limiter,
|
|
|
|
lbry_session.blob_manager, self.blind_info_manager,
|
|
|
|
lbry_session.wallet, self.payment_rate_manager)
|
|
|
|
return self.repeater.setup()
|
|
|
|
|
|
|
|
def _restore_repeater_status(self, lbry_session):
|
|
|
|
d = self.settings.get_saved_max_space()
|
|
|
|
|
|
|
|
def set_max_space(max_space):
|
|
|
|
self.repeater.set_max_space(max_space)
|
|
|
|
|
|
|
|
d.addCallback(set_max_space)
|
|
|
|
|
|
|
|
d.addCallback(lambda _: self.settings.get_approved_peers())
|
|
|
|
|
|
|
|
def set_approved_peers(peers):
|
|
|
|
for host, port in peers:
|
|
|
|
peer = lbry_session.peer_manager.get_peer(host, int(port))
|
|
|
|
self.repeater.add_approved_peer(peer)
|
|
|
|
|
|
|
|
d.addCallback(set_approved_peers)
|
|
|
|
|
|
|
|
d.addCallback(lambda _: self.settings.get_repeater_saved_status())
|
|
|
|
|
|
|
|
def restore_running(running):
|
|
|
|
if running:
|
|
|
|
return self.repeater.start()
|
|
|
|
else:
|
|
|
|
return defer.succeed(True)
|
|
|
|
|
|
|
|
d.addCallback(restore_running)
|
|
|
|
return d
|
|
|
|
|
|
|
|
def _create_control_handlers(self, lbry_session):
|
|
|
|
category = "Blind Repeater"
|
|
|
|
control_handlers = [StartRepeaterFactory(self.repeater, self.settings),
|
|
|
|
StopRepeaterFactory(self.repeater, self.settings),
|
|
|
|
RepeaterStatusFactory(self.repeater),
|
|
|
|
ShowApprovedPeersFactory(self.repeater),
|
|
|
|
ModifyRepeaterOptionsFactory(self.repeater, lbry_session, self.settings)]
|
|
|
|
self.control_handlers = zip([category] * len(control_handlers), control_handlers)
|
|
|
|
|
|
|
|
def _add_to_lbry_console(self, lbry_console):
|
|
|
|
lbry_console.add_control_handlers(self.control_handlers)
|
|
|
|
lbry_console.add_query_handlers([self.valuable_blob_length_query_handler,
|
2015-09-08 21:42:56 +02:00
|
|
|
self.valuable_blob_hash_query_handler])
|