From bcd026a1b66ceb5d623a8f2a300df0fea690baf6 Mon Sep 17 00:00:00 2001 From: Jack Robison Date: Mon, 13 Feb 2017 16:48:02 -0500 Subject: [PATCH] add only-free payment rate manager for testing --- lbrynet/core/PaymentRateManager.py | 45 +++++++++++++++++++++++++++++- lbrynet/core/PriceModel.py | 8 ++++++ lbrynet/core/Strategy.py | 18 +++++++++++- 3 files changed, 69 insertions(+), 2 deletions(-) diff --git a/lbrynet/core/PaymentRateManager.py b/lbrynet/core/PaymentRateManager.py index 491c3ed79..cdda9b630 100644 --- a/lbrynet/core/PaymentRateManager.py +++ b/lbrynet/core/PaymentRateManager.py @@ -1,4 +1,4 @@ -from lbrynet.core.Strategy import get_default_strategy +from lbrynet.core.Strategy import get_default_strategy, OnlyFreeStrategy from lbrynet import conf from decimal import Decimal @@ -81,3 +81,46 @@ class NegotiatedPaymentRateManager(object): return (offer.is_too_low and round(Decimal.from_float(offer.rate), 5) >= round(self.strategy.max_rate, 5)) return False + + +class OnlyFreePaymentsManager(object): + 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 + 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 diff --git a/lbrynet/core/PriceModel.py b/lbrynet/core/PriceModel.py index 844a0e2c7..aad6eb42f 100644 --- a/lbrynet/core/PriceModel.py +++ b/lbrynet/core/PriceModel.py @@ -9,6 +9,14 @@ def get_default_price_model(blob_tracker, base_price, **kwargs): return MeanAvailabilityWeightedPrice(blob_tracker, base_price, **kwargs) +class ZeroPrice(object): + def __init__(self): + self.base_price = 0.0 + + def calculate_price(self, blob): + return 0.0 + + class MeanAvailabilityWeightedPrice(object): """Calculate mean-blob-availability and stream-position weighted price for a blob diff --git a/lbrynet/core/Strategy.py b/lbrynet/core/Strategy.py index 4ae620f4a..0ee0d1efd 100644 --- a/lbrynet/core/Strategy.py +++ b/lbrynet/core/Strategy.py @@ -3,7 +3,7 @@ from decimal import Decimal from lbrynet import conf from lbrynet.interfaces import INegotiationStrategy from lbrynet.core.Offer import Offer -from lbrynet.core.PriceModel import MeanAvailabilityWeightedPrice +from lbrynet.core.PriceModel import MeanAvailabilityWeightedPrice, ZeroPrice def get_default_strategy(blob_tracker, **kwargs): @@ -134,3 +134,19 @@ class BasicAvailabilityWeightedStrategy(Strategy): with_premium = self._premium(rate, offer_count) rounded_price = round(with_premium, 5) return self._bounded_price(rounded_price) + + +class OnlyFreeStrategy(Strategy): + implementer(INegotiationStrategy) + def __init__(self, *args, **kwargs): + price_model = ZeroPrice() + Strategy.__init__(self, price_model, 0.0, 0.0, True) + + def _get_mean_rate(self, rates): + return 0.0 + + def _get_response_rate(self, rates, offer_count): + return 0.0 + + def _make_rate_offer(self, rates, offer_count): + return 0.0