From ba7af5e77ff2cd1b3314f43947d27786a2edfdde Mon Sep 17 00:00:00 2001 From: James Riehl <33920192+jrriehl@users.noreply.github.com> Date: Tue, 22 Aug 2023 15:28:53 +0100 Subject: [PATCH] fix: registration exceptions and retries (#128) --- python/.pylintrc | 3 ++- python/src/uagents/agent.py | 20 ++++++++++++-------- python/src/uagents/config.py | 5 +++-- python/src/uagents/network.py | 8 ++++---- 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/python/.pylintrc b/python/.pylintrc index e2c6445a..bd151bd4 100644 --- a/python/.pylintrc +++ b/python/.pylintrc @@ -8,5 +8,6 @@ disable=missing-module-docstring, too-many-instance-attributes, duplicate-code, too-many-arguments, - logging-fstring-interpolation + logging-fstring-interpolation, + broad-exception-caught diff --git a/python/src/uagents/agent.py b/python/src/uagents/agent.py index 9a58e174..af611c60 100644 --- a/python/src/uagents/agent.py +++ b/python/src/uagents/agent.py @@ -28,8 +28,9 @@ from uagents.mailbox import MailboxClient from uagents.config import ( REGISTRATION_FEE, - MIN_REGISTRATION_TIME, + REGISTRATION_UPDATE_INTERVAL_SECONDS, LEDGER_PREFIX, + REGISTRATION_RETRY_INTERVAL_SECONDS, parse_endpoint_config, parse_agentverse_config, get_logger, @@ -241,7 +242,8 @@ async def register(self): # or anything has changed from the last registration if ( not self._almanac_contract.is_registered(self.address) - or self._schedule_registration() < MIN_REGISTRATION_TIME + or self._almanac_contract.get_expiry(self.address) + < REGISTRATION_UPDATE_INTERVAL_SECONDS or self._endpoints != self._almanac_contract.get_endpoints(self.address) or list(self.protocols.keys()) != self._almanac_contract.get_protocols(self.address) @@ -271,15 +273,17 @@ async def register(self): self._logger.info("Almanac registration is up to date!") async def _registration_loop(self): - await self.register() - # schedule the next registration + time_until_next_registration = REGISTRATION_UPDATE_INTERVAL_SECONDS + try: + await self.register() + except Exception as ex: + self._logger.exception(f"Failed to register on almanac contract: {ex}") + time_until_next_registration = REGISTRATION_RETRY_INTERVAL_SECONDS + # schedule the next registration update self._loop.create_task( - _delay(self._registration_loop(), self._schedule_registration()) + _delay(self._registration_loop(), time_until_next_registration) ) - def _schedule_registration(self): - return self._almanac_contract.get_expiry(self.address) - def on_interval( self, period: float, diff --git a/python/src/uagents/config.py b/python/src/uagents/config.py index b56e3414..c4867098 100644 --- a/python/src/uagents/config.py +++ b/python/src/uagents/config.py @@ -22,8 +22,9 @@ class AgentNetwork(Enum): ) REGISTRATION_FEE = 500000000000000000 REGISTRATION_DENOM = "atestfet" -MIN_REGISTRATION_TIME = 3600 -BLOCK_INTERVAL = 5 +REGISTRATION_UPDATE_INTERVAL_SECONDS = 3600 +REGISTRATION_RETRY_INTERVAL_SECONDS = 60 +AVERAGE_BLOCK_INTERVAL = 5.7 AGENT_NETWORK = AgentNetwork.FETCHAI_TESTNET AGENTVERSE_URL = "https://agentverse.ai" diff --git a/python/src/uagents/network.py b/python/src/uagents/network.py index f9488a97..caa19277 100644 --- a/python/src/uagents/network.py +++ b/python/src/uagents/network.py @@ -22,7 +22,7 @@ CONTRACT_ALMANAC, CONTRACT_NAME_SERVICE, AGENT_NETWORK, - BLOCK_INTERVAL, + AVERAGE_BLOCK_INTERVAL, REGISTRATION_FEE, REGISTRATION_DENOM, get_logger, @@ -80,19 +80,19 @@ def is_registered(self, address: str) -> bool: return False return True - def get_expiry(self, address: str): + def get_expiry(self, address: str) -> int: query_msg = {"query_records": {"agent_address": address}} response = self.query(query_msg) if not response["record"]: contract_state = self.query({"query_contract_state": {}}) expiry = contract_state.get("state").get("expiry_height") - return expiry * BLOCK_INTERVAL + return expiry * AVERAGE_BLOCK_INTERVAL expiry = response.get("record")[0].get("expiry") height = response.get("height") - return (expiry - height) * BLOCK_INTERVAL + return (expiry - height) * AVERAGE_BLOCK_INTERVAL def get_endpoints(self, address: str): query_msg = {"query_records": {"agent_address": address}}