Skip to content

Commit

Permalink
Fix message count calculation (#615)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeandemeusy authored Jan 28, 2025
1 parent c358a45 commit 8f657eb
Show file tree
Hide file tree
Showing 14 changed files with 27 additions and 97 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ out.mp4
websocket_client.log
logs/
*.log
.logs/

#misc
.coverage
Expand Down
15 changes: 3 additions & 12 deletions ct-app/core/api/hoprd_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
Message,
OpenedChannel,
TicketPrice,
TicketProbability,
)

MESSAGE_TAG = 0x1245
Expand Down Expand Up @@ -211,11 +210,11 @@ async def node_info(self) -> Optional[Infos]:

async def ticket_price(self) -> Optional[TicketPrice]:
"""
Gets the ticket price set by the oracle.
Gets the ticket price set in the configuration file.
:return: TicketPrice
"""
is_ok, response = await self.__call_api(HTTPMethod.GET, "network/price")
return TicketPrice(response) if is_ok else None
is_ok, response = await self.__call_api(HTTPMethod.GET, "node/configuration")
return TicketPrice(Configuration(json.loads(response)).as_dict) if is_ok else None

async def messages_pop_all(self, tag: int = MESSAGE_TAG) -> list:
"""
Expand All @@ -228,14 +227,6 @@ async def messages_pop_all(self, tag: int = MESSAGE_TAG) -> list:
)
return [Message(item) for item in response.get("messages", [])] if is_ok else []

async def winning_probability(self) -> Optional[TicketProbability]:
"""
Gets the winning probability set in the HOPRd node configuration file.
:return: TicketProbability
"""
is_ok, response = await self.__call_api(HTTPMethod.GET, "node/configuration")
return TicketProbability(Configuration(json.loads(response)).as_dict) if is_ok else None

async def healthyz(self, timeout: int = 20) -> bool:
"""
Checks if the node is healthy. Return True if `healthyz` returns 200 after max `timeout` seconds.
Expand Down
14 changes: 7 additions & 7 deletions ct-app/core/api/response_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,23 +107,23 @@ def post_init(self):
self.value = float(self.value) / 1e18


class TicketProbability(ApiResponseObject):
keys = {"value": "probability"}
class Configuration(ApiResponseObject):
keys = {
"price": "hopr/protocol/outgoing_ticket_price"
}

def post_init(self):
self.value = float(self.value)


class Configuration(ApiResponseObject):
keys = {"probability": "hopr/protocol/outgoing_ticket_winning_prob"}
self.price = float(self.price.split()[0])


class OpenedChannel(ApiResponseObject):
keys = {"channel_id": "channelId", "receipt": "transactionReceipt"}


class Message(ApiResponseObject):
keys = {"body": "body", "timestamp": "timestamp"}


class Channels:
def __init__(self, data: dict):
self.all = [Channel(channel) for channel in data.get("all", [])]
Expand Down
45 changes: 0 additions & 45 deletions ct-app/core/components/budget.py

This file was deleted.

12 changes: 2 additions & 10 deletions ct-app/core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,25 +350,17 @@ async def peers_rewards(self):
@master(flagguard, formalin)
async def ticket_parameters(self):
"""
Gets the ticket price and winning probability from the api. They are used in the economic model to calculate the number of messages to send to a peer.
Gets the ticket price from the api. They are used in the economic model to calculate the number of messages to send to a peer.
"""
ticket_price = await self.api.ticket_price()
if ticket_price is None:
self.warning("Ticket price not available.")
return

win_probability = await self.api.winning_probability()
if win_probability is None:
self.warning("Winning probability not available.")
return

self.debug(
f"Ticket price: {ticket_price.value}, winning probability: {win_probability.value}"
)
self.debug(f"Ticket price: {ticket_price.value}")

for model in self.models.values():
model.budget.ticket_price = ticket_price.value
model.budget.winning_probability = win_probability.value

@master(flagguard, formalin)
async def safe_fundings(self):
Expand Down
13 changes: 1 addition & 12 deletions ct-app/core/economic_model/budget.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,13 @@
class Budget:
def __init__(self):
self.ticket_price = None
self.winning_probability = None

@property
def ticket_price(self):
return self._ticket_price

@property
def winning_probability(self):
return self._winning_probability

@ticket_price.setter
def ticket_price(self, value):
if value is not None:
self._ticket_price = value
TICKET_STATS.labels("price").set(value)

@winning_probability.setter
def winning_probability(self, value):
if value is not None:
self._winning_probability = value
TICKET_STATS.labels("win_prob").set(value)
TICKET_STATS.labels("price").set(value)
4 changes: 1 addition & 3 deletions ct-app/core/economic_model/economic_model_legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ def yearly_message_count(self, stake: float, redeemed_rewards: float = 0):
rewards = self.apr * self.transformed_stake(stake) / 100
self.coefficients.c -= redeemed_rewards

under = self.budget.ticket_price * self.budget.winning_probability

return rewards / under * self.proportion if under != 0 else 0
return rewards / self.budget.ticket_price * self.proportion

@classmethod
def fromParameters(cls, parameters: Parameters):
Expand Down
3 changes: 1 addition & 2 deletions ct-app/core/economic_model/economic_model_sigmoid.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,8 @@ def yearly_message_count(self, stake: float, xs: list[float]):
apr = self.apr(xs)

rewards = apr * stake / 100.0
under = self.budget.ticket_price * self.budget.winning_probability

return round(rewards / under * self.proportion) if under != 0 else 0
return rewards / self.budget.ticket_price * self.proportion

@classmethod
def fromParameters(cls, parameters: Parameters):
Expand Down
6 changes: 5 additions & 1 deletion ct-app/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,9 @@ for i in $(seq 1 $count); do
export NODE_KEY_${i}=$TOKEN
done

# create log folder
mkdir -p .logs
time=$(date '+%Y%m%d_%H%M%S')

echo "Starting core in $env mode"
python -m core --configfile ./.configs/core_${env}_config.yaml 2>&1 | tee logs_core.log
python -m core --configfile ./.configs/core_${env}_config.yaml 2>&1 | tee ".logs/core_$time.log"
1 change: 0 additions & 1 deletion ct-app/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ def inbox_messages(self, *args, **kwargs):
def budget() -> Budget:
budget = Budget()
budget.ticket_price = 0.0001
budget.winning_probability = 1
return budget


Expand Down
2 changes: 1 addition & 1 deletion ct-app/test/model/test_economic_model_sigmoid.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,5 @@ def test_yearly_message_count(budget: Budget):
model.apr([0.5, 0.25])
/ 100
* stake
/ (budget.ticket_price * budget.winning_probability)
/ (budget.ticket_price)
)
3 changes: 2 additions & 1 deletion helm/values-prod-blue.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
replicas: 5
network: dufour
version: "2.2.1-pr.6791"
version: "2.2.1"

deployment:
env: |
Expand Down Expand Up @@ -33,6 +33,7 @@ config: |
threshold: 40
protocol:
outgoing_ticket_winning_prob: 1
outgoing_ticket_price: "1500000000000000 HOPR"
heartbeat:
timeout: 7
network_options:
Expand Down
2 changes: 1 addition & 1 deletion helm/values-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ backup:
ctdapp:
core:
replicas: 1
tag: v3.7.1
tag: v3.7.2

nodes:
NODE_ADDRESS_1: http://ctdapp-blue-node-1:3001
Expand Down
3 changes: 2 additions & 1 deletion helm/values-staging-green.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
replicas: 5
network: rotsee
version: singapore-latest
version: "2.2.1"

deployment:
env: |
Expand All @@ -24,6 +24,7 @@ config: |
strategies: []
protocol:
outgoing_ticket_winning_prob: 1
outgoing_ticket_price: "200 HOPR"
inbox:
capacity: 16384
Expand Down

0 comments on commit 8f657eb

Please sign in to comment.