2016-10-13 19:35:55 +02:00
|
|
|
from zope.interface import implementer
|
2016-10-06 04:58:34 +02:00
|
|
|
from decimal import Decimal
|
2016-10-27 21:18:25 +02:00
|
|
|
from lbrynet.conf import settings
|
2016-10-13 19:35:55 +02:00
|
|
|
from lbrynet.interfaces import INegotiationStrategy
|
2016-09-27 19:52:44 +02:00
|
|
|
from lbrynet.core.Offer import Offer
|
2016-10-06 04:58:34 +02:00
|
|
|
from lbrynet.core.PriceModel import MeanAvailabilityWeightedPrice
|
2016-09-27 19:52:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_default_strategy(blob_tracker, **kwargs):
|
|
|
|
return BasicAvailabilityWeightedStrategy(blob_tracker, **kwargs)
|
|
|
|
|
|
|
|
|
2016-10-13 19:35:55 +02:00
|
|
|
class Strategy(object):
|
|
|
|
"""
|
|
|
|
Base for negotiation strategies
|
|
|
|
"""
|
|
|
|
implementer(INegotiationStrategy)
|
|
|
|
|
2016-10-26 09:16:33 +02:00
|
|
|
def __init__(self, price_model, max_rate, min_rate, is_generous=settings.is_generous_host):
|
2016-10-06 04:58:34 +02:00
|
|
|
self.price_model = price_model
|
|
|
|
self.is_generous = is_generous
|
|
|
|
self.accepted_offers = {}
|
2016-10-20 07:35:18 +02:00
|
|
|
self.pending_sent_offers = {}
|
2016-10-06 04:58:34 +02:00
|
|
|
self.offers_sent = {}
|
|
|
|
self.offers_received = {}
|
2016-10-20 18:29:55 +02:00
|
|
|
self.max_rate = max_rate or Decimal(self.price_model.base_price * 50)
|
2016-10-06 04:58:34 +02:00
|
|
|
self.min_rate = Decimal(min_rate)
|
2016-09-27 19:52:44 +02:00
|
|
|
|
2016-10-13 19:35:55 +02:00
|
|
|
def _make_rate_offer(self, rates, offer_count):
|
|
|
|
return NotImplementedError()
|
2016-10-06 04:58:34 +02:00
|
|
|
|
2016-10-13 19:35:55 +02:00
|
|
|
def _get_response_rate(self, rates, offer_count):
|
|
|
|
return NotImplementedError()
|
2016-10-06 04:58:34 +02:00
|
|
|
|
|
|
|
def make_offer(self, peer, blobs):
|
|
|
|
offer_count = self.offers_sent.get(peer, 0)
|
2016-10-13 19:35:55 +02:00
|
|
|
self._add_offer_sent(peer)
|
2016-10-06 04:58:34 +02:00
|
|
|
if peer in self.accepted_offers:
|
|
|
|
# if there was a previous accepted offer, use that
|
|
|
|
offer = self.accepted_offers[peer]
|
2016-10-20 07:35:18 +02:00
|
|
|
if peer in self.pending_sent_offers:
|
|
|
|
del self.pending_sent_offers[peer]
|
2016-10-06 04:58:34 +02:00
|
|
|
elif offer_count == 0 and self.is_generous:
|
|
|
|
# Try asking for it for free
|
|
|
|
offer = Offer(Decimal(0.0))
|
2016-10-20 07:35:18 +02:00
|
|
|
self.pending_sent_offers.update({peer: offer})
|
2016-10-06 04:58:34 +02:00
|
|
|
else:
|
2016-10-13 19:35:55 +02:00
|
|
|
rates = [self.price_model.calculate_price(blob) for blob in blobs]
|
|
|
|
price = self._make_rate_offer(rates, offer_count)
|
|
|
|
offer = Offer(price)
|
2016-10-20 07:35:18 +02:00
|
|
|
self.pending_sent_offers.update({peer: offer})
|
2016-10-06 04:58:34 +02:00
|
|
|
return offer
|
|
|
|
|
2016-09-27 19:52:44 +02:00
|
|
|
def respond_to_offer(self, offer, peer, blobs):
|
2016-10-06 04:58:34 +02:00
|
|
|
offer_count = self.offers_received.get(peer, 0)
|
2016-10-13 19:35:55 +02:00
|
|
|
self._add_offer_received(peer)
|
|
|
|
rates = [self.price_model.calculate_price(blob) for blob in blobs]
|
|
|
|
price = self._get_response_rate(rates, offer_count)
|
2016-10-06 04:58:34 +02:00
|
|
|
if peer in self.accepted_offers:
|
|
|
|
offer = self.accepted_offers[peer]
|
|
|
|
elif offer.rate == 0.0 and offer_count == 0 and self.is_generous:
|
2016-09-28 05:56:08 +02:00
|
|
|
# give blobs away for free by default on the first request
|
|
|
|
offer.accept()
|
2016-10-06 04:58:34 +02:00
|
|
|
self.accepted_offers.update({peer: offer})
|
2016-10-13 19:35:55 +02:00
|
|
|
elif offer.rate >= price:
|
2016-09-27 19:52:44 +02:00
|
|
|
offer.accept()
|
2016-10-06 04:58:34 +02:00
|
|
|
self.accepted_offers.update({peer: offer})
|
2016-09-27 19:52:44 +02:00
|
|
|
else:
|
|
|
|
offer.reject()
|
2016-10-06 04:58:34 +02:00
|
|
|
if peer in self.accepted_offers:
|
|
|
|
del self.accepted_offers[peer]
|
|
|
|
return offer
|
2016-09-27 19:52:44 +02:00
|
|
|
|
2016-10-13 19:35:55 +02:00
|
|
|
def update_accepted_offers(self, peer, offer):
|
|
|
|
if not offer.is_accepted and peer in self.accepted_offers:
|
|
|
|
del self.accepted_offers[peer]
|
|
|
|
if offer.is_accepted:
|
|
|
|
self.accepted_offers.update({peer: offer})
|
2016-10-20 18:29:55 +02:00
|
|
|
self.pending_sent_offers.update({peer: offer})
|
2016-09-28 05:56:08 +02:00
|
|
|
|
2016-10-13 19:35:55 +02:00
|
|
|
def _add_offer_sent(self, peer):
|
|
|
|
turn = self.offers_sent.get(peer, 0) + 1
|
|
|
|
self.offers_sent.update({peer: turn})
|
|
|
|
|
|
|
|
def _add_offer_received(self, peer):
|
|
|
|
turn = self.offers_received.get(peer, 0) + 1
|
|
|
|
self.offers_received.update({peer: turn})
|
|
|
|
|
|
|
|
def _bounded_price(self, price):
|
|
|
|
price_for_return = Decimal(min(self.max_rate, max(price, self.min_rate)))
|
|
|
|
return price_for_return
|
2016-09-27 19:52:44 +02:00
|
|
|
|
|
|
|
|
2016-10-13 19:35:55 +02:00
|
|
|
class BasicAvailabilityWeightedStrategy(Strategy):
|
2016-11-30 21:20:45 +01:00
|
|
|
"""Basic strategy to target blob prices based on supply relative to mean supply
|
|
|
|
|
|
|
|
Discount price target with each incoming request, and raise it
|
|
|
|
with each outgoing from the modeled price until the rate is
|
|
|
|
accepted or a threshold is reached
|
2016-10-06 04:58:34 +02:00
|
|
|
|
|
|
|
"""
|
2016-10-13 19:35:55 +02:00
|
|
|
implementer(INegotiationStrategy)
|
2016-10-06 04:58:34 +02:00
|
|
|
|
2016-11-30 21:20:45 +01:00
|
|
|
def __init__(self, blob_tracker, acceleration=1.25,
|
|
|
|
deceleration=0.9, max_rate=None,
|
|
|
|
min_rate=0.0,
|
|
|
|
is_generous=settings.is_generous_host,
|
|
|
|
base_price=0.0001, alpha=1.0):
|
|
|
|
price_model = MeanAvailabilityWeightedPrice(
|
|
|
|
blob_tracker, base_price=base_price, alpha=alpha)
|
2016-10-13 19:35:55 +02:00
|
|
|
Strategy.__init__(self, price_model, max_rate, min_rate, is_generous)
|
|
|
|
self._acceleration = Decimal(acceleration) # rate of how quickly to ramp offer
|
2016-10-06 04:58:34 +02:00
|
|
|
self._deceleration = Decimal(deceleration)
|
|
|
|
|
|
|
|
def _get_mean_rate(self, rates):
|
|
|
|
mean_rate = Decimal(sum(rates)) / Decimal(max(len(rates), 1))
|
|
|
|
return mean_rate
|
2016-09-27 19:52:44 +02:00
|
|
|
|
|
|
|
def _premium(self, rate, turn):
|
2016-10-06 04:58:34 +02:00
|
|
|
return rate * (self._acceleration ** Decimal(turn))
|
2016-09-27 19:52:44 +02:00
|
|
|
|
|
|
|
def _discount(self, rate, turn):
|
2016-10-06 04:58:34 +02:00
|
|
|
return rate * (self._deceleration ** Decimal(turn))
|
|
|
|
|
2016-10-13 19:35:55 +02:00
|
|
|
def _get_response_rate(self, rates, offer_count):
|
2016-10-06 04:58:34 +02:00
|
|
|
rate = self._get_mean_rate(rates)
|
|
|
|
discounted = self._discount(rate, offer_count)
|
2016-10-13 19:35:55 +02:00
|
|
|
rounded_price = round(discounted, 5)
|
|
|
|
return self._bounded_price(rounded_price)
|
2016-10-06 04:58:34 +02:00
|
|
|
|
2016-10-13 19:35:55 +02:00
|
|
|
def _make_rate_offer(self, rates, offer_count):
|
2016-10-06 04:58:34 +02:00
|
|
|
rate = self._get_mean_rate(rates)
|
|
|
|
with_premium = self._premium(rate, offer_count)
|
2016-10-13 19:35:55 +02:00
|
|
|
rounded_price = round(with_premium, 5)
|
|
|
|
return self._bounded_price(rounded_price)
|