2018-11-04 20:06:29 +01:00
|
|
|
from lbrynet.p2p.Strategy import get_default_strategy, OnlyFreeStrategy
|
2016-12-21 20:55:43 +01:00
|
|
|
from lbrynet import conf
|
2016-10-20 18:29:55 +02:00
|
|
|
from decimal import Decimal
|
2016-10-19 06:12:44 +02:00
|
|
|
|
2016-10-06 04:58:34 +02:00
|
|
|
|
2018-07-22 00:34:59 +02:00
|
|
|
class BasePaymentRateManager:
|
2016-12-21 20:55:43 +01:00
|
|
|
def __init__(self, rate=None, info_rate=None):
|
2017-01-17 04:23:20 +01:00
|
|
|
self.min_blob_data_payment_rate = rate if rate is not None else conf.settings['data_rate']
|
2016-12-21 20:55:43 +01:00
|
|
|
self.min_blob_info_payment_rate = (
|
2017-01-17 04:23:20 +01:00
|
|
|
info_rate if info_rate is not None else conf.settings['min_info_rate'])
|
2015-08-20 17:27:15 +02:00
|
|
|
|
|
|
|
|
2018-07-22 00:34:59 +02:00
|
|
|
class PaymentRateManager:
|
2015-08-20 17:27:15 +02:00
|
|
|
def __init__(self, base, rate=None):
|
|
|
|
"""
|
|
|
|
@param base: a BasePaymentRateManager
|
|
|
|
|
|
|
|
@param rate: the min blob data payment rate
|
|
|
|
"""
|
|
|
|
self.base = base
|
|
|
|
self.min_blob_data_payment_rate = rate
|
|
|
|
self.points_paid = 0.0
|
|
|
|
|
|
|
|
def get_rate_blob_data(self, peer):
|
|
|
|
return self.get_effective_min_blob_data_payment_rate()
|
|
|
|
|
|
|
|
def accept_rate_blob_data(self, peer, payment_rate):
|
|
|
|
return payment_rate >= self.get_effective_min_blob_data_payment_rate()
|
|
|
|
|
|
|
|
def get_effective_min_blob_data_payment_rate(self):
|
|
|
|
if self.min_blob_data_payment_rate is None:
|
|
|
|
return self.base.min_blob_data_payment_rate
|
|
|
|
return self.min_blob_data_payment_rate
|
|
|
|
|
|
|
|
def record_points_paid(self, amount):
|
2016-09-27 19:52:44 +02:00
|
|
|
self.points_paid += amount
|
|
|
|
|
|
|
|
|
2018-07-22 00:34:59 +02:00
|
|
|
class NegotiatedPaymentRateManager:
|
2016-12-21 20:55:43 +01:00
|
|
|
def __init__(self, base, availability_tracker, generous=None):
|
2016-09-27 19:52:44 +02:00
|
|
|
"""
|
|
|
|
@param base: a BasePaymentRateManager
|
|
|
|
@param availability_tracker: a BlobAvailabilityTracker
|
|
|
|
@param rate: the min blob data payment rate
|
|
|
|
"""
|
|
|
|
|
|
|
|
self.base = base
|
2017-05-29 22:38:08 +02:00
|
|
|
self.min_blob_data_payment_rate = base.min_blob_data_payment_rate
|
2016-09-27 19:52:44 +02:00
|
|
|
self.points_paid = 0.0
|
|
|
|
self.blob_tracker = availability_tracker
|
2017-01-17 04:23:20 +01:00
|
|
|
self.generous = generous if generous is not None else conf.settings['is_generous_host']
|
2016-10-13 19:35:55 +02:00
|
|
|
self.strategy = get_default_strategy(self.blob_tracker,
|
|
|
|
base_price=self.base.min_blob_data_payment_rate,
|
|
|
|
is_generous=generous)
|
2016-09-27 19:52:44 +02:00
|
|
|
|
|
|
|
def get_rate_blob_data(self, peer, blobs):
|
|
|
|
response = self.strategy.make_offer(peer, blobs)
|
|
|
|
return response.rate
|
|
|
|
|
|
|
|
def accept_rate_blob_data(self, peer, blobs, offer):
|
2016-10-06 04:58:34 +02:00
|
|
|
offer = self.strategy.respond_to_offer(offer, peer, blobs)
|
2016-10-13 19:35:55 +02:00
|
|
|
self.strategy.update_accepted_offers(peer, offer)
|
|
|
|
return offer.is_accepted
|
2016-10-06 04:58:34 +02:00
|
|
|
|
|
|
|
def reply_to_offer(self, peer, blobs, offer):
|
|
|
|
reply = self.strategy.respond_to_offer(offer, peer, blobs)
|
2016-10-13 19:35:55 +02:00
|
|
|
self.strategy.update_accepted_offers(peer, reply)
|
2016-10-06 04:58:34 +02:00
|
|
|
return reply
|
|
|
|
|
|
|
|
def get_rate_for_peer(self, peer):
|
|
|
|
return self.strategy.accepted_offers.get(peer, False)
|
2016-09-27 19:52:44 +02:00
|
|
|
|
|
|
|
def record_points_paid(self, amount):
|
|
|
|
self.points_paid += amount
|
2016-10-06 04:58:34 +02:00
|
|
|
|
|
|
|
def record_offer_reply(self, peer, offer):
|
2016-10-20 18:29:55 +02:00
|
|
|
self.strategy.update_accepted_offers(peer, offer)
|
|
|
|
|
|
|
|
def price_limit_reached(self, peer):
|
|
|
|
if peer in self.strategy.pending_sent_offers:
|
|
|
|
offer = self.strategy.pending_sent_offers[peer]
|
2016-11-30 21:20:45 +01:00
|
|
|
return (offer.is_too_low and
|
|
|
|
round(Decimal.from_float(offer.rate), 5) >= round(self.strategy.max_rate, 5))
|
|
|
|
return False
|
2017-02-13 22:48:02 +01:00
|
|
|
|
|
|
|
|
2018-07-22 00:34:59 +02:00
|
|
|
class OnlyFreePaymentsManager:
|
2017-02-13 22:48:02 +01:00
|
|
|
def __init__(self, **kwargs):
|
|
|
|
"""
|
|
|
|
A payment rate manager that will only ever accept and offer a rate of 0.0,
|
|
|
|
Used for testing
|
|
|
|
"""
|
|
|
|
|
|
|
|
self.base = BasePaymentRateManager(0.0, 0.0)
|
|
|
|
self.points_paid = 0.0
|
2018-05-23 22:51:25 +02:00
|
|
|
self.min_blob_data_payment_rate = 0.0
|
2017-02-13 22:48:02 +01:00
|
|
|
self.generous = True
|
|
|
|
self.strategy = OnlyFreeStrategy()
|
|
|
|
|
|
|
|
def get_rate_blob_data(self, peer, blobs):
|
|
|
|
response = self.strategy.make_offer(peer, blobs)
|
|
|
|
return response.rate
|
|
|
|
|
|
|
|
def accept_rate_blob_data(self, peer, blobs, offer):
|
|
|
|
offer = self.strategy.respond_to_offer(offer, peer, blobs)
|
|
|
|
self.strategy.update_accepted_offers(peer, offer)
|
|
|
|
return offer.is_accepted
|
|
|
|
|
|
|
|
def reply_to_offer(self, peer, blobs, offer):
|
|
|
|
reply = self.strategy.respond_to_offer(offer, peer, blobs)
|
|
|
|
self.strategy.update_accepted_offers(peer, reply)
|
|
|
|
return reply
|
|
|
|
|
|
|
|
def get_rate_for_peer(self, peer):
|
|
|
|
return self.strategy.accepted_offers.get(peer, False)
|
|
|
|
|
|
|
|
def record_points_paid(self, amount):
|
|
|
|
self.points_paid += amount
|
|
|
|
|
|
|
|
def record_offer_reply(self, peer, offer):
|
|
|
|
self.strategy.update_accepted_offers(peer, offer)
|
|
|
|
|
|
|
|
def price_limit_reached(self, peer):
|
|
|
|
if peer in self.strategy.pending_sent_offers:
|
|
|
|
offer = self.strategy.pending_sent_offers[peer]
|
|
|
|
if offer.rate > 0.0:
|
|
|
|
return True
|
|
|
|
return False
|