From 5db96d000632fb8490ea329d05bbd92e02d5f408 Mon Sep 17 00:00:00 2001 From: Jean Demeusy Date: Fri, 7 Jun 2024 16:06:35 +0200 Subject: [PATCH] legacy economic model tests --- ct-app/core/model/economic_model_legacy.py | 11 +- ct-app/scripts/core_prod_config.yaml | 1 - ct-app/scripts/core_production_config.yaml | 139 ------------------ .../test/model/test_economic_model_legacy.py | 47 +++++- 4 files changed, 49 insertions(+), 149 deletions(-) delete mode 100644 ct-app/scripts/core_production_config.yaml diff --git a/ct-app/core/model/economic_model_legacy.py b/ct-app/core/model/economic_model_legacy.py index 691dfe62..4b98d952 100644 --- a/ct-app/core/model/economic_model_legacy.py +++ b/ct-app/core/model/economic_model_legacy.py @@ -61,14 +61,15 @@ def __init__( self.budget: Budget = None def transformed_stake(self, stake: float): - func = self.equations.f_x - # convert parameters attribute to dictionary kwargs = vars(self.coefficients) kwargs.update({"x": stake}) - if not eval(func.condition, kwargs): - func = self.equations.g_x + for func in vars(self.equations).values(): + if eval(func.condition, kwargs): + break + else: + return 0 return eval(func.formula, kwargs) @@ -80,7 +81,7 @@ def message_count_for_reward(self, stake: float): denominator = self.budget.ticket_price * self.budget.winning_probability if denominator != 0: - return round(rewards / denominator * self.economic_model.proportion) + return round(rewards / denominator * self.proportion) else: return 0 diff --git a/ct-app/scripts/core_prod_config.yaml b/ct-app/scripts/core_prod_config.yaml index 47dd9e54..a6d085a9 100644 --- a/ct-app/scripts/core_prod_config.yaml +++ b/ct-app/scripts/core_prod_config.yaml @@ -56,7 +56,6 @@ economicModel: budget: amount: 190000 period: 2628000 # in seconds - s: 1 countsInPeriod: 1460 winningProbability: 1 diff --git a/ct-app/scripts/core_production_config.yaml b/ct-app/scripts/core_production_config.yaml deleted file mode 100644 index 8b19bc31..00000000 --- a/ct-app/scripts/core_production_config.yaml +++ /dev/null @@ -1,139 +0,0 @@ ---- -# ============================================================================= -# -# ============================================================================= -flags: - core: - healthcheck: 10 - checkSubgraphURLs: 30 - getFundings: 30 - getTicketPrice: 30 - aggregatePeers: 30 - getTopologyData: 30 - getSubgraphData: 30 - getRegisteredNodes: 30 - getNFTHolders: 30 - getPeersRewards: 30 - applyEconomicModel: 30 - distributeRewards: ~ - - - node: - healthcheck: 10 - retrievePeers: 30 - retrieveIncomingChannels: 30 - retrieveOutgoingChannels: 30 - retrieveBalances: 30 - openChannels: ~ - closeOldChannels: ~ - closePendingChannels: ~ - fundChannels: ~ - closeIncomingChannels: ~ - getTotalChannelFunds: ~ - -# ============================================================================= -# -# ============================================================================= -economicModel: - minSafeAllowance: -1 - NFTThreshold: ~ - - legacy: - proportion: 0.9 - apr: 12.0 - - coefficients: - a: 1 - b: 1 - c: 3 - l: 0 - - equations: - fx: - formula: "a * x" - condition: "l <= x <= c" - gx: - formula: "a * c + (x - c) ** (1 / b)" - condition: "x > c" - - sigmoid: - proportion: 0.1 - maxAPRPercentage: 15.0 - offset: 10 - networkCapacity: 2000 - totalTokenSupply: 1000000000 - - buckets: - economicSecurity: - flatness: 1.65 - skewness: 1.50 - upperbound: 0.5 - networkCapacity: - flatness: 10.0 - skewness: 2.75 - upperbound: 1.0 - - budget: - period: 2628000 - countsInPeriod: 365 - winningProbability: 1 - -# ============================================================================= -# -# ============================================================================= -distribution: - minEligiblePeers: 500 - messageDeliveryDelay: 10.0 - delayBetweenTwoMessages: 0.2 - batchSize: 100 - maxIterations: 6 - -# ============================================================================= -# -# ============================================================================= -gcp: - filePrefix: expected_reward - folder: refactored_app - bucket: ct-platform-ct - -# ============================================================================= -# -# ============================================================================= -peer: - minVersion: '2.0.7' - -# ============================================================================= -# -# ============================================================================= -channel: - minBalance: 0.05 - fundingAmount: 0.2 - maxAgeSeconds: 60 - -# ============================================================================= -# -# ============================================================================= -rabbitmq: - taskName: fake_task - projectName: ct-app - -# ============================================================================= -# -# ============================================================================= -subgraph: - safesBalance: - queryID: FEQcaX9qfh31YL2K7rxRN5a3sr9rjMWkguJnby7StNRo - URLBackup: https://api.studio.thegraph.com/query/40439/hopr-nodes-dufour/version/latest - - staking: - queryID: DrkbaCvNGVcNH1RghepLRy6NSHFi8Dmwp4T2LN3LqcjY - URLBackup: https://api.studio.thegraph.com/query/40439/hopr-stake-all-seasons/v0.0.10 - - wxHOPRTxs: - queryID: ~ - URLBackup: https://api.studio.thegraph.com/query/58438/wxhoprtransactions/v0.0.6 - - rewards: - queryID: ~ #Feg6Jero3aQzesVYuqk253NNLyNAZZppbDPKFYEGJ1Hj - URLBackup: https://api.studio.thegraph.com/query/40439/hopr-channels/version/latest -... \ No newline at end of file diff --git a/ct-app/test/model/test_economic_model_legacy.py b/ct-app/test/model/test_economic_model_legacy.py index ed3e77cc..8ccd6a1b 100644 --- a/ct-app/test/model/test_economic_model_legacy.py +++ b/ct-app/test/model/test_economic_model_legacy.py @@ -1,8 +1,47 @@ import pytest -from core.components.parameters import Parameters from core.model.budget import Budget -from core.model.economic_model_legacy import EconomicModelLegacy +from core.model.economic_model_legacy import ( + Coefficients, + EconomicModelLegacy, + Equation, + Equations, +) -def test_init_class(): - pass +@pytest.fixture +def model(): + model = EconomicModelLegacy( + Equations( + Equation("a * x", "l <= x <= c"), + Equation("a * c + (x - c) ** (1 / b)", "x > c"), + ), + Coefficients(1, 2, 3, 1), + 1, + 20.0, + ) + model.budget = Budget(1200, 1, 1) + model.budget.ticket_price = 0.1 + + return model + + +def test_transformed_stake(model: EconomicModelLegacy): + assert model.transformed_stake(0) == 0 + assert model.transformed_stake(model.coefficients.l) == model.coefficients.l + assert model.transformed_stake(model.coefficients.c) == model.coefficients.c + assert model.transformed_stake(model.coefficients.c * 2) < ( + model.coefficients.c * 2 + ) + + +def test_message_count_for_reward(model: EconomicModelLegacy): + assert model.message_count_for_reward(0) == 0, "No reward for 0 stake" + assert model.message_count_for_reward(model.coefficients.l) == round( + model.coefficients.l * model.apr / model.budget.ticket_price / 12 + ), "Linear result if stake is minimum" + assert model.message_count_for_reward(model.coefficients.c) == round( + model.coefficients.c * model.apr / model.budget.ticket_price / 12 + ), "Linear result if stake is at threshold" + assert model.message_count_for_reward(model.coefficients.c * 2) < round( + (model.coefficients.c + 2) * model.apr / model.budget.ticket_price / 12 + ), "Non-linear result if stake is above threshold"